- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在业余时间从事一个网站项目一段时间,最近决定需要 Web 浏览器来检测 Web 服务器是否已宕机。使用了很多 AJAX,因此我最初尝试了一些相当简单的代码,涉及 XMLHttpRequest 对象的“responseText”字段。如果它是空的,我会假设服务器已关闭,并关闭网页。
想象一下,当有时responseText字段为空时,即使我知道我的测试服务器已经完全“启动”,我也会感到惊讶。服务器日志表明,显然 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;
}
关于javascript - 如何使用 AJAX 检测 Web 服务器是否宕机或未接收请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22292117/
我已经为我的 PHP 代码设置了一个 Cron 作业,每 20 分钟运行一次。但它每次都会杀死 EC2 t2micro 实例。这是服务器日志。请帮忙。 ip-172-31-42-52 login: [
将 MassTransit 配置为容错以处理间歇性 RabbitMQ 连接问题或 RabbitMQ 完全崩溃的正确方法是什么?我已经在两台不同的机器之间设置了一个 RabbitMQ 集群并配置了 HA
我们的 Tomcat 6.0.29 在达到最大线程数后宕机。我真的很感激任何帮助,因为它是一个生产服务器。 这是 catalina.log 文件的一部分: INFO: Maximum number o
Redis 在我的应用程序中是可选的,这是即使 Redis 关闭,应用程序必须能够毫无问题地启动的要求之一。 我能够用 spring-data-redis 处理这个问题 1.8.1 版本,但升级到 时
Kafka宕机引发的高可用问题 问题要从一次Kafka的宕机开始说起。 笔者所在的是一家金融科技公司,但公司内部并没有采用在金融支付领域更为流行的RabbitMQ,而是采用了设计之初就为日志处理
我们正在运行 Spark 2.4.0/Scala 2.11,并且运行一些监听 Kafka 主题的 Spark 流应用程序。 它是 Spark Kafka Direct 流 API,我们正在运行 4 个
如果我有一个 MongoDB 副本集,其中有 3 个节点(主要节点、次要节点、仲裁节点),并且主要节点出现故障,现在次要节点成为主要节点,您如何动态处理客户端中的更改,以便它们现在写入基本的? 我在开
我正在运行 Rails 3.1.0 应用程序,但遇到了一个奇怪的问题。在我们的登台服务器上,只有很少的事件,我们有 5 个 ruby 进程不断地使用以下命令 ping mySQL: poll([{
我是一名优秀的程序员,十分优秀!