XMLHttpRequest.status always returning 0
html
<a href="#" onclick="MyObj.startup()">click me</a>
js code开发者_如何学C
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.
You can overcome 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.org/En/Using_XMLHttpRequest#section_3.
精彩评论