我正在尝试使用 Ajax 来告诉我我的服务器是否启动。我只用一次 Ajax 调用就制作了一个简单的页面。当服务器启动时,它会返回 xmlhttp.responseText。如果服务器关闭了,它应该显示“SERVER DOWN”...但是当我加载页面,然后关闭 Apache 时,它仍然显示服务器已启动。我还应该采取其他方法吗?
<!DOCTYPE html>
<html>
<head>
<script>
var xmlhttp;
var url = "http://192.168.0.5/ajax_info.txt";
function loadXMLDoc(url,cfunc)
{
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=cfunc;
xmlhttp.open("GET",url,true);
xmlhttp.send();
}
function myFunction()
{
loadXMLDoc(url,function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
}
else
{
document.getElementById("myDiv").innerHTML = "Server Down!";
}
});
}
</script>
</head>
<body>
<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="myFunction()">Change Content</button>
</body>
</html>
感谢您的帮助
最佳答案
您的页面可能已被缓存,请在网址中使用缓存破坏器。例如
xmlhttp.open("GET",url+'?_dc='+(new Date()).getTime(),true);
关于javascript - 无法从 Ajax 调用获取 'bad response',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15456737/