如何使用AJAX来检测web服务器是否关闭或未接收请求
How to use AJAX to detect if the web server is down or not receiving requests
我在业余时间做了一段时间的网站项目,最近决定我需要一个web浏览器来检测web服务器是否宕机了。这里使用了大量AJAX,所以我最初尝试了一些相当简单的代码,涉及XMLHttpRequest对象的"responseText"字段。如果它是空的,我将假设服务器已关闭,并关闭网页。
想象一下我的惊讶,有时responseText字段是空的,即使我知道我的test-Server是完全"up"。服务器日志表明,AJAX请求显然有可能无法到达服务器——但是客户端,即浏览器,仍然将XMLHttpRequest对象的"readyState"字段设置为"4"(表示成功)。
这意味着我需要足够健壮的代码来处理这种让我惊讶的情况,以及我最初想要测试的情况——关于Web服务器实际上已经关闭。
这里有一些代码似乎工作得很好(不包括我在提取/简化它以在这里发布时可能犯的任何拼写错误)。
var AJobj, invl, tri, coun, str, rs; //some handy global variables
function initialize() //called by "onLoad" event handler for the HTML <body> tag
{ window.name="MyWindow";
if (window.XMLHttpRequest)
{ if (AJobj == null) //prevent creating AJAX object more than once
AJobj = new XMLHttpRequest();
}
else //display message about browser not being recent enough
{ window.open("Sorry.htm", "MyWindow", true);
return;
}
tri = 0; //number-of-tries counter
invl = false; //either the handle of an interval timer, or a no-timer flag
//other initialization stuff, for the web page, goes here
return;
}
function getAJAXdata1(a)
{ if(a != "a") //test for NOT "again"
{ if(invl !== false) //if interval timer already running
return; //ignore calls to this function if waiting for another AJAX request to complete
invl = setInterval("AJAXready(1);", 500); //start an interval timer for this AJAX request
coun = 0; //initialize counter associated with the interval timer
}
else
{ ; //when this function called "again", the goal is to duplicate the original AJAX request
; //If you need to do something special to ensure request is duplicated, it goes here
}
try
{ AJobj.open("GET", "datagroup1.php?info1=test&info2=thoroughly" , true); //sample AJAX request ('GET' type)
}
catch(err) //if an error happens that the client can detect
{ if(invl !== false)
clearInterval(invl); //turn off interval timer because leaving page
window.open("Sorry2.htm", "MyWindow", true); //load appropriate error-message page
}
AJobj.onreadystatechange = function(){AJAXready(1);}; //anonymous callback function
// is called when Web Server responds, and sends a parameter to a specific function below
AJobj.send(null); //complete the send-off of the AJAX request
return;
}
function useAJAXdata1()
{ ; //variable "str" holds the data received from the Web Server
; //do what you want with it
return;
}
//Below, the differences from 'getAJAXdata1(a)" are the name of this function,
// the parameter in the callback function for the interval timer,
// the name of the PHP file that gets called to provide AJAX data,
// the URL data being sent to that PHP file for processing,
// and the parameter in the anonymous callback function.
function getAJAXdata2(a)
{ if(a != "a")
{ if(invl !== false)
return;
invl = setInterval("AJAXready(2);", 500);
coun = 0;
}
else //function parameter specifies "again"
{ ; //if you need to do something special to ensure an
; // AJAX request is exactly duplicated, it goes here
}
try
{ AJobj.open("GET", "datagroup2.php?info1=thoroughly&info2=test" , true);
}
catch(err)
{ if(invl !== false)
clearInterval(invl);
window.open("Sorry2.htm", "MyWindow", true);
}
AJobj.onreadystatechange = function(){AJAXready(2);};
AJobj.send(null);
return;
}
function useAJAXdata2()
{ ; //variable "str" holds the data received from the Web Server
; //do what you want with it
return;
}
//SIMILARLY, a third, fourth, and so on, "getAJAXdata(a)" and "useAJAXdata()" function could be created if needed
//This function is called by the interval timer AND by a successful AJAX request
function AJAXready(w) //"w" refers to "which", such as getAJAXdata1() or getAJAXdata2()
{ rs = AJobj.readyState; //get status of AJAX request
if(rs < 4) //if not yet completed successfully
{ if(coun++ > 15) //15 calls from the interval timer (about 7 1/2 seconds; pick time you think best)
{ coun = 0; //reset the counter of calls originating with the interval timer
TryAgain(w); //Try sending a duplicate of the original AJAX request,
} // for which the response that took too long
return;
}
str = AJobj.responseText;//Client thinks AJAX request succeeded, so fetch the data sent by the Web Server
if(str == "") //SURPRISE! Sometimes there is no data!
//(server logs can indicate that the AJAX request never actually arrived)
{ coun = 0; //reset interval-timer counter
TryAgain(w); //try sending a duplicate of the original AJAX request
return;
}
//ACTUAL SUCCESS if JavaScript processing reaches this code
if(invl !== false)
{ clearInterval(invl); //turn off the interval timer
invl = false; //reset the flag
}
tri = 0; //reset counters
coun = 0;
eval("useAJAXdata"+w+"()"); //call the appropriate "useAJAXdata" function
return;
}
function TryAgain(w)
{ if(tri++ > 2)
{ if(invl != false)
clearInterval(invl); //turn off interval timer
alert("Three attempts to reach the Web Server have'n" +
"failed; it may be down. (The rest of this'n" +
"message is up to you.)");
window.close(); //or you could send the user to some other web site
// window.open("http://www.stackoverflow.com", "MyWindow", true);
}
else //Call the appropriate "getAJAXdata" function, with
eval("getAJAXdata"+w+'("a")'); // parameter "a" for "again", to get data from Web Server
return;
}
相关文章:
- 我的jQuery加载请求是否被调用了两次
- Web 工作线程中的同步 XHR 请求是否仍会锁定浏览器
- 从 Web Worker 执行 AJAX 请求是否可行
- 如何在mvc3视图javascript中查找当前请求是否为ajax
- 如何检查AJAX请求是否已完成
- 如何使用请求 npm 模块判断 http 请求是否完成
- 确定 Node .js 应用程序中的请求是否为本地请求
- 如何验证请求是否来自您网站上的脚本
- 检查 ajax 请求是否在大多数情况下失败
- 检查请求是否来自网络而不是手动
- Mongodb 找到一个,但首先检查请求是否为空
- PHP中有什么方法可以检测请求是否来自浏览器后退按钮
- 检查请求是否被用户使用 javascript 阻止
- 如何验证请求是否来自特定 Web 应用
- 在大多数情况下,POST 请求是否可以替代 GET 请求
- 测试ajax请求是否尚未以角度发出
- AJAX请求是否覆盖另一个请求
- 从客户端发出instagram API请求是否安全
- 如何判断请求是否应该呈现在服务器端?(ReactJS)
- JavaScript:失败的图像请求是否有error属性