1

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.com/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!!

cheng
  • 6,596
  • 6
  • 25
  • 27

3 Answers3

6

Check that you are not making a cross domain request.

If for example you are not serving this page from http://www.spartanbook.com 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.

Gavin
  • 2,153
  • 2
  • 25
  • 35
  • Gavin, I CAN access the content by browser, but can't do it from code. so I guess it won't relate to domain. – cheng Jun 07 '11 at 19:55
  • 3
    @user788072: That's exactly the point-- cross-domain policy applies to JavaScript asynchronous requests in JavaScript code. Of course you can access it in your browser-- your browser can access any domain! – Platinum Azure Jun 07 '11 at 19:58
  • 2
    +1 to offset the possible ill-informed downvote, until whoever downvoted comes out and explains why and there's actually a good reason. – Platinum Azure Jun 07 '11 at 19:59
  • Hi Cheng. You need to pass your request through a local proxy. Please take a look at this link as an example. [link](http://jquery-howto.blogspot.com/2009/04/cross-domain-ajax-querying-with-jquery.html) – Gavin Jun 07 '11 at 20:12
0

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.org/wiki/Same_origin_policy

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

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.

Community
  • 1
  • 1
Gruber
  • 4,478
  • 6
  • 47
  • 74
0

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

Hasan Fahim
  • 3,875
  • 1
  • 30
  • 51