问题status=0很蛋疼啊~!!!

kuawai 2013-08-14 03:36:10
新手学习javascript好容易行进到Ajax结果就来了这个
window.onload = initAll;
var xhr = false;

function initAll() {
document.getElementById("makeTextRequest").onclick = getNewFile;
document.getElementById("makeXMLRequest").onclick = getNewFile;
}

function getNewFile() {
makeRequest(this.href);
return false;
}

function makeRequest(url) {
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
}
else {
if (window.ActiveXObject) {
try {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) { }
}
}

if (xhr) {
xhr.onreadystatechange = showContents;
xhr.open("GET", url, true);
xhr.send(null);
}
else {
document.getElementById("updateArea").innerHTML = "Sorry, but I couldn't create an XMLHttpRequest";
}
}

function showContents() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
if (xhr.responseXML && xhr.responseXML.childNodes.length > 0) {
var outMsg = getText(xhr.responseXML.getElementsByTagName("choices")[0]);
}
else {
var outMsg = xhr.responseText;
}
}
else {
var outMsg = "There was a problem with the request " + xhr.status;
}这里,老是要跳转到这里。
document.getElementById("updateArea").innerHTML = outMsg;
alert("sadasdasd");我弄了个alert才能看到他调到这报告status=0.然后整个页面就变成我请求文本的那个页面,是在搞不懂求高手解答。
}

function getText(inVal) {
if (inVal.textContent) {
return inVal.textContent;
}
return inVal.text;
}
}
...全文
615 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Go 旅城通票 2013-08-15
  • 打赏
  • 举报
回复
本地测试,未发布的站点请求成功status就是0,要发布网站通过http或者https访问才是200状态,要增加status=0判断,但是为0的时候IE下的xml生成不了DOM树,参考这里:http://bbs.csdn.net/topics/310120750 function showContents() { if (xhr.readyState == 4) { if (xhr.status == 200||0==xhr.status) { if (xhr.responseXML && xhr.responseXML.childNodes.length > 0) { var outMsg = getText(xhr.responseXML.getElementsByTagName("choices")[0]); } else { var outMsg = xhr.responseText; } document.getElementById("updateArea").innerHTML = outMsg; } else { var outMsg = "There was a problem with the request " + xhr.status; } ‘’ document.getElementById("updateArea").innerHTML = outMsg; alert("sadasdasd"); } function getText(inVal) { if (inVal.textContent) { return inVal.textContent; } return inVal.text; } }
Go 旅城通票 2013-08-15
  • 打赏
  • 举报
回复
如果不是动态页,不需要用web服务器,动态页就必须搭建服务器了或者使用软件自带的小型服务器来测试。
kuawai 2013-08-15
  • 打赏
  • 举报
回复
引用 5 楼 showbo 的回复:
本地测试,未发布的站点请求成功status就是0,要发布网站通过http或者https访问才是200状态,要增加status=0判断,但是为0的时候IE下的xml生成不了DOM树,参考这里:http://bbs.csdn.net/topics/310120750 function showContents() { if (xhr.readyState == 4) { if (xhr.status == 200||0==xhr.status) { if (xhr.responseXML && xhr.responseXML.childNodes.length > 0) { var outMsg = getText(xhr.responseXML.getElementsByTagName("choices")[0]); } else { var outMsg = xhr.responseText; } document.getElementById("updateArea").innerHTML = outMsg; } else { var outMsg = "There was a problem with the request " + xhr.status; } ‘’ document.getElementById("updateArea").innerHTML = outMsg; alert("sadasdasd"); } function getText(inVal) { if (inVal.textContent) { return inVal.textContent; } return inVal.text; } }
谢谢大神,我知道了原来我那个东西是没有放在服务器上,如果弄在服务器上好像会很麻烦,大神能不能给点服务器的参考指南什么的
kuawai 2013-08-15
  • 打赏
  • 举报
回复
回复5楼这个else是和status一堆的呀
persuit666 2013-08-14
  • 打赏
  • 举报
回复
你那个else 是和 xhr.readyState 一对,应该是和xhr.status == 200这个一对才行 if (xhr.readyState == 4) { if (xhr.status == 200) { if (xhr.responseXML && xhr.responseXML.childNodes.length > 0) outMsg = getText(xhr.responseXML.getElementsByTagName("choices")[0]); else outMsg = "There was a problem with the request " + xhr.status; } }
kuawai 2013-08-14
  • 打赏
  • 举报
回复
this.href有值啊,就是下面代码<a id="makeTextRequest" href="gAddress.txt">Request a text file</a><br> <a id="makeXMLRequest" href="us-states.xml">Request an XML file</a>
街头小贩 2013-08-14
  • 打赏
  • 举报
回复

function getNewFile() {
makeRequest(this.href);
return false;
}
this.href有值吗
kuawai 2013-08-14
  • 打赏
  • 举报
回复
Html 是这样的 <!DOCTYPE html> <html> <head> <title>My First Ajax Script</title> <script src="script01.js"></script> </head> <body> <p> <a id="makeTextRequest" href="gAddress.txt">Request a text file</a><br> <a id="makeXMLRequest" href="us-states.xml">Request an XML file</a> </p> <div id="updateArea"> </div> </body> </html>

52,797

社区成员

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

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