I want to write a function for my website, which checks several web servers to see if they are online or not... I'm working with jQuery.ajax() I realized that I have to use JSONP, but i really dont know how to do that! I have an array of IP adresses and I want to check if the corresponding servers are available.
Here is my code so far:
function urlExists(url){
$.ajax({
timeout: 5000,
type: 'GET',
dataType: 'jsonp',
url: "http://" + url,
cache: false,
"error":function(XMLHttpRequest,textStatus, errorThrown) {
if(errorThrown == "timeout") {
alert("woah, there we got a timeout...");
// At this point the request shall abort
}
},
statusCode: {
404:function(){
alert("404!");
},
0:function(){
alert("0!");
},
500:function(){
alert("500!");
},
200:function(){
alert("it worked!");
}
}
});
}
urlExists("192.168.5.55");
After that i want to save whether the webserver is online or not, but where?
You can use ajax complete event to check if all the links are tested or not
function urlExists(url){
$.ajax({
timeout: 5000,
type: 'GET',
dataType: 'jsonp',
url: "http://" + url,
cache: false,
"error":function(XMLHttpRequest,textStatus, errorThrown) {
if(errorThrown == "timeout") {
result.push({
'url': url,
'status': "woah, there we got a timeout..."
});
// At this point the request shall abort
}
},
complete: function(){
// Handle the complete event
if(result.length == list.length) {
alert("All url's checked. Check the console for 'result' varialble");
console.log(result);
}
},
statusCode: {
404:function(){
result.push({
'url': url,
'status': "404!"
});
},
0:function(){
result.push({
'url': url,
'status': "0!"
});
},
500:function(){
result.push({
'url': url,
'status': "500"
});
},
200:function(){
result.push({
'url': url,
'status': "it worked!"
});
}
}
});
}
var result = new Array;
var list = ['192.168.5.55','192.168.98.99'];
for (var i = list.length - 1; i >= 0; i--) {
urlExists(list[i]);
};
你必须调用一个php函数,你有url:“http://”+ url,(url是你的控制器+方法),这里做验证状态并插入你的数据库
The only way to do this is involves a hackish solution like this: Is it possible to ping a server from Javascript?
Since you would like to save the results, I am going to assume you are using a server-side language such as PHP or ASP. I am familiar with PHP so I would recommend getting PHP to execute a PING command and figure out if the server is online or not based on that result.
EDIT
In regards to the first comment I would recommend implementing an httprequest instead of PING.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.