日期:2014-05-16  浏览次数:20632 次

问题status=0很蛋疼啊~!!!
本帖最后由 showbo 于 2013-08-15 09:18:21 编辑
新手学习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;
}
}
JavaScript XMLHttpRequest Ajax

------解决方案--------------------
本地测试,未发布的站点请求成功status就是0,要发布网站通过http或者https访问才是200状态,要增加status=0判断,但是为0的时候IE下的xml生成不了DOM树,参考这里:http://bbs.csdn.net/topics/310120750