AJAX返回status的值经常是-1

绿野仙踪 绿野仙踪 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
代码如下,不知道各位有没有遇见过这种问题。代码不是运行在IE里,是widget。开发文档里写的支持javascript,但是用ajax请求数据时,有时候就会出xhr.status = -1 的情况,其他时候正常返回status =200 。请各位大侠帮忙解答

function getNews(id,url,type,sort)
{
var xhr;
//call the right constructor for the browser being used
if (window.ActiveXObject)
xhr = new ActiveXObject("Microsoft.XMLHTTP");
else if (window.XMLHttpRequest)
xhr = new XMLHttpRequest();
else
alert("AJAX request not supported");

//prepare the xmlhttprequest object
xhr.open("GET", url, true);
xhr.setRequestHeader("Cache-Control", "no-cache");
xhr.setRequestHeader("Pragma", "no-cache");
xhr.onreadystatechange = function() {
if (xhr.readyState == 4)
{
if (xhr.status == 200)
{
if (xhr.responseText != null)
{
//process sina news
processNews(xhr.responseXML, id,type,sort,url);
}
else
{
alert("Failed to receive RSS file from the server - file not found.");
return false;
}
}
else
{
alert("Error code " + xhr.status + " received: " + xhr.statusText);
}
}
}
//send the request
xhr.send(null);
}

...全文

相关推荐

评论 1
收藏 已收藏
分享
返回顶部
绿野仙踪
JavaScript 87,984 成员 224,680 内容