初学AJAX遇到问题了,有没有热心的大牛哥帮忙看一下代码555

loveqq1943 2010-01-30 10:41:29
首先编写一个AJAX的js文件,取名 ajax.js

// global variables to keep track of the request
// and the function to call when done
var ajaxreq=false, ajaxCallback;
// ajaxRequest: Sets up a request
function ajaxRequest(filename) {
try {
// Firefox / IE7 / Others
ajaxreq= new XMLHttpRequest();
} catch (error) {
try {
// IE 5 / IE 6
ajaxreq = new ActiveXObject("Microsoft.XMLHTTP");
} catch (error) {
return false;
}
}
ajaxreq.open("GET",filename);
ajaxreq.onreadystatechange = ajaxResponse;
ajaxreq.send(null);
}
// ajaxResponse: Waits for response and calls a function
function ajaxResponse() {
if (ajaxreq.readyState !=4) return;
if (ajaxreq.status==200) {
// if the request succeeded...
if (ajaxCallback) ajaxCallback();
} else alert("Request failed: " + ajaxreq.statusText);
return true;
}

然后是HTML

<html>
<head><title>Ajax Test</title>
<script language="JavaScript" type="text/javascript"
src="ajax.js">
</script>
</head>
<body>
<h1>Ajax Quiz Example</h1>
<form>
<p><b>Question:</b>
<span id="question">...
</span>
</p>
<p><b>Answer:</b>
<input type="text" name="answer" id="answer">
<input type="button" value="Submit" id="submit">
</p>
<input type="button" value="Start the Quiz" id="startq">
</form>
<script language="JavaScript" type="text/javascript"
src="quiz.js">
</script>
</body>
</html>

脚本quiz.js的代码:
// global variable qn is the current question number
var qn=0;
// load the questions from the XML file
function getQuestions() {
obj=document.getElementById("question");
obj.firstChild.nodeValue="(please wait)";
ajaxCallback = nextQuestion;
ajaxRequest("questions.xml");
}
// display the next question
function nextQuestion() {
questions = ajaxreq.responseXML.getElementsByTagName("q");
obj=document.getElementById("question");
if (qn < questions.length) {
q = questions[qn].firstChild.nodeValue;
obj.firstChild.nodeValue=q;
} else {
obj.firstChild.nodeValue="(no more questions)";
}
}
// check the user's answer
function checkAnswer() {
answers = ajaxreq.responseXML.getElementsByTagName("a");
a = answers[qn].firstChild.nodeValue;
answerfield = document.getElementById("answer");
if (a == answerfield.value) {
alert("Correct!");
}
else {
alert("Incorrect. The correct answer is: " + a);
}
qn = qn + 1;
answerfield.value="";
nextQuestion();
}
// Set up the event handlers for the buttons
obj=document.getElementById("startq");
obj.onclick=getQuestions;
ans=document.getElementById("submit");
ans.onclick=checkAnswer;


xml的内容为:
<?xml version="1.0" encoding="gb2312"?>
<questions>
<q>What DOM object contains URL information for the window?</q>
<a>location</a>
<q>Which method of the document object finds the objext for an element?</q>
<a>getElementById</a>
<q>If you declare a variable outside a function,is it global or local?</q>
<a>global</a>
<q>What is the formal standard for the JavaScript language called</q>
<a>ECMAScript</a>
</questions>

把这四个文件放在了同一个文件夹下面了已经,这个程序是一个在线问答的程序,为什么总是提示ajax拒绝呢?
...全文
141 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
云•海 2010-02-02
  • 打赏
  • 举报
回复
ajax.js中有一行错误代码ajaxreq.open("GET",filename); 应该是ajaxreq.open("GET",filename,true);
happy664618843 2010-02-01
  • 打赏
  • 举报
回复
firebug 调式
mengshan1986 2010-02-01
  • 打赏
  • 举报
回复
下个火狐装上firebug,自己调试就可以了
中智软件科技 2010-02-01
  • 打赏
  • 举报
回复
加群学习 交流:69705156
  • 打赏
  • 举报
回复
哪行报错??
如果你是直接拖进浏览器浏览,在ie6【没装其他版本,不知道会不会出错】下会出错,因为请求xml文件时在ie下无法生成xml的dom树

自己对比下是否还有其他的问题
ajax问题总结
KK3K2005 2010-01-31
  • 打赏
  • 举报
回复
你的AJAX代码在网站上应该是可以访问的
loveqq1943 2010-01-30
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 icaac 的回复:]
积分总是不够
[/Quote]

555555555555我的积分都浪费在C语言版了,最近研究JS严重积分不足
ICAAC 2010-01-30
  • 打赏
  • 举报
回复
积分总是不够
ICAAC 2010-01-30
  • 打赏
  • 举报
回复
好复杂啊

52,783

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧