简体   繁体   English

无法从Ajax呼叫获得“不良响应”

[英]Cannot get a 'bad response' from Ajax call

I'm trying to use Ajax to tell me whether my server is up or not. 我正在尝试使用Ajax告诉我服务器是否启动。 I've made a simple page with just one Ajax call. 我仅用一个Ajax调用就制作了一个简单的页面。 When the server is up, it comes back with the xmlhttp.responseText. 服务器启动后,它会返回xmlhttp.responseText。 IF the server is down it is supposed to say "SERVER DOWN"...but when I load the page, then turn off Apache, it still says that the server is up. 如果服务器已关闭,则应该说“ SERVER DOWN” ...但是当我加载页面,然后关闭Apache时,它仍然表示服务器已启动。 Is there some other way I should be doing this? 我还有其他方法可以这样做吗?

<!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>

Thanks for the help 谢谢您的帮助

Your page is probably cached, use a cache buster in the url. 您的页面可能已缓存,请在URL中使用缓存清除器。 eg 例如

xmlhttp.open("GET",url+'?_dc='+(new Date()).getTime(),true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM