最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ajax status = 0 - Stack Overflow

programmeradmin1浏览0评论

i'm trying to get information from a remote server on my local machine. readyState has no problem, i.e. ==4. however, status is always 0(instead of 200) when I hit the button, it returns nothing.

here is the code,:

<html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET",'.asp?control=campus&campus=45&term=80',true);
xmlhttp.send();
}
</script>
</head>
<body>

<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>

</body>
</html>

It's basically from w3shcools. simply replaced the url. the url I'm using is working when I paste it into address bar of my browser.

Any idea? Thanks!!

i'm trying to get information from a remote server on my local machine. readyState has no problem, i.e. ==4. however, status is always 0(instead of 200) when I hit the button, it returns nothing.

here is the code,:

<html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET",'http://www.spartanbook./textbooks_xml.asp?control=campus&campus=45&term=80',true);
xmlhttp.send();
}
</script>
</head>
<body>

<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>

</body>
</html>

It's basically from w3shcools. simply replaced the url. the url I'm using is working when I paste it into address bar of my browser.

Any idea? Thanks!!

Share Improve this question edited Jun 7, 2011 at 19:39 cheng asked Jun 7, 2011 at 19:29 chengcheng 6,6967 gold badges26 silver badges27 bronze badges 2
  • 2 "Any idea?": Don't use w3schools :-) w3fools. – Platinum Azure Commented Jun 7, 2011 at 19:32
  • 1 It doesn't work, because W3Schools is bad. – McKayla Commented Jun 7, 2011 at 19:33
Add a ment  | 

3 Answers 3

Reset to default 6

Check that you are not making a cross domain request.

If for example you are not serving this page from http://www.spartanbook. then the expected result would be access denied, which oddly enough gives a readyState of 4, but a status of 0.

If you need to make a cross domain request, then you need to use a proxy.

This error code indicates that the response was empty. Seems like a firewall issue

Like Gavin says, it seems your problem is that you're making a cross domain request, which is blocked by most browsers due to security reasons: http://en.wikipedia/wiki/Same_origin_policy

I suggest you move the code from your local puter to a place which satisfies the "Same Origin Policy", in your case that is somewhere on www.spartanbook..

However, there is a workaround for the problem, and that is to use JSONP. That will allow you to fetch JSON data from a program on another domain.

发布评论

评论列表(0)

  1. 暂无评论