我有一个简单的 HTML 文件,它从服务器获取数据并输出:
<html>
<head>
<script type="text/javascript">
var xmlhttp = new XMLHttpRequest();
function startRequest() {
xmlhttp.onreadystatechange = handleStateChange;
xmlhttp.open("GET", "http://new-host-2.home/test.html", true);
xmlhttp.send(null);
}
function handleStateChange() {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
alert("The server replied with: ", xmlhttp.responseText);
}
else {
alert(xmlhttp.status);
}
}
}
</script>
</head>
<body>
<form>
<input type="button" value="Woooo" onclick="startRequest()"/>
</form>
</body>
</html>
服务器上的文件 test.html 如下所示:
<h1>Data received!</h1>
尽管在控制台中,它说一切都很好,但我一直得到 0 作为状态,并给出 200 状态。当我更改if (xmlhttp.status == 200)
为 时if (xmlhttp.status == 0)
,它只是输出The server replied with:
。为什么是这样?我在搞砸什么吗?
编辑:它可能只是我的服务器,我要切换到不同的服务器。标头可能会有所帮助:
Response Headers
Accept-Ranges bytes
Connection Keep-Alive
Content-Length 13
Content-Type text/html
Date Sat, 09 Jun 2012 14:17:11 GMT
Etag "267749b-d-4c20a6d0ef180"
Keep-Alive timeout=15, max=100
Last-Modified Sat, 09 Jun 2012 13:52:22 GMT
Server Apache/2.2.19 (Unix) DAV/2
Request Headers
Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language en-us,en;q=0.5
Connection keep-alive
Host new-host-2.home
Origin null
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:13.0) Gecko/20100101 Firefox/13.0