[英]Cannot get a 'bad response' from Ajax call
我正在嘗試使用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>
謝謝您的幫助
您的頁面可能已緩存,請在URL中使用緩存清除器。 例如
xmlhttp.open("GET",url+'?_dc='+(new Date()).getTime(),true);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.