javascript 阿贾克斯状态= 0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6270553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 20:06:16  来源:igfitidea点击:

ajax status = 0

javascriptajaxstatus

提问by cheng

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.

我正在尝试从本地机器上的远程服务器获取信息。readyState 没有问题,即 ==4。然而,当我按下按钮时,状态总是 0(而不是 200),它什么都不返回。

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.

它基本上来自 w3shcools。简单地替换了网址。当我将其粘贴到浏览器的地址栏中时,我正在使用的 url 正在工作。

Any idea? Thanks!!

任何的想法?谢谢!!

回答by Gavin

Check that you are not making a cross domain request.

检查您没有发出跨域请求。

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

例如,如果您不是从http://www.spartanbook.com提供此页面,那么预期的结果将是访问被拒绝,奇怪的是,readyState 为 4,但状态为 0。

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

如果需要进行跨域请求,则需要使用代理。

回答by Hasan Fahim

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

此错误代码表示响应为空。好像是防火墙问题

回答by Gruber

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

就像 Gavin 说的那样,您的问题似乎是您正在发出跨域请求,由于安全原因,大多数浏览器都阻止了该请求: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.

我建议您将代码从本地计算机移动到满足“同源策略”的地方,在您的情况下是 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.

但是,该问题有一个解决方法,那就是使用JSONP。这将允许您从另一个域上的程序中获取 JSON 数据。