阿贾克斯状态= 0状态、阿贾克斯

2023-09-10 14:43:17 作者:带刀爷们儿

我试图从我的本地机器远程服务器获取信息。 readyState的没有问题的,即== 4。然而,状态始终为0(而不是200) 当我打的按钮,它没有返回值。

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.

这里是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>

这基本上是从w3shcools。简单地更换了网址。 当我将其粘贴到浏览器的地址栏中我使用的URL是否正常。

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.

你知道吗? 谢谢!

推荐答案

请检查你是不是做跨域请求。

Check that you are not making a cross domain request.

例如,如果你不从 http://www.spartanbook.com 服此页则预期的结果将是访问被拒绝,这奇怪的是给出了一个readyState的4个,但为0的状态。

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.