gpt4 book ai didi

javascript - Ajax 状态= 0

转载 作者:行者123 更新时间:2023-11-30 08:14:16 26 4
gpt4 key购买 nike

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

代码如下:

<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 有效。

有什么想法吗?谢谢!!

最佳答案

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

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

如果需要跨域请求,那么就需要使用代理。

关于javascript - Ajax 状态= 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6270553/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com