admin管理员组文章数量:1330614
html
<a href="#" onclick="MyObj.startup()">click me</a>
js code
var MyObj =
{
startup : function()
{
var ajax = null;
ajax = new XMLHttpRequest();
ajax.open('GET', '', true);
ajax.onreadystatechange = function(evt)
{
if(ajax.readyState == 4)
{
if (ajax.status == 200)
{
window.dump(":)\n");
}
else
{
window.dump(":(\n");
}
}
}
ajax.send(null);
}
}
ajax.status
always returning 0, no matter which site it is, no matter what is the actual return code. I say actual, because ajax.statusText
returning correct value, eg OK or Redirecting...
ajax.readyState
also returns proper values and 4 at the end.
html
<a href="#" onclick="MyObj.startup()">click me</a>
js code
var MyObj =
{
startup : function()
{
var ajax = null;
ajax = new XMLHttpRequest();
ajax.open('GET', 'http://www.nasa.gov', true);
ajax.onreadystatechange = function(evt)
{
if(ajax.readyState == 4)
{
if (ajax.status == 200)
{
window.dump(":)\n");
}
else
{
window.dump(":(\n");
}
}
}
ajax.send(null);
}
}
ajax.status
always returning 0, no matter which site it is, no matter what is the actual return code. I say actual, because ajax.statusText
returning correct value, eg OK or Redirecting...
ajax.readyState
also returns proper values and 4 at the end.
2 Answers
Reset to default 4You can overe this easily in a local environment by setting up a php proxy (xampp a server and pass a querystring for the url you want to grab). Have your php proxy wget the url and echo its contents. That way your local html file (when viewed as http://localhost/your.html) can send ajax requests out of domain all day. Just don't expect the content to work as though it were local to that domain.
Is your site part of http://www.nasa.gov/? Otherwise, XMLHttpRequest will fail due to Same Origin Policy.
Also, if the page is served as a non-HTTP request, the status
can be 0. See https://developer.mozilla/En/Using_XMLHttpRequest#section_3.
本文标签: javascriptXMLHttpRequeststatus always returning 0Stack Overflow
版权声明:本文标题:javascript - XMLHttpRequest.status always returning 0 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742222963a2435569.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论