简体   繁体   English

jQuery来检查服务器可用性

[英]jQuery to check server availability

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! 我想为我的网站编写一个函数,它检查几个Web服务器以查看它们是否在线...我正在使用jQuery.ajax()我意识到我必须使用JSONP,但我真的不知道怎么做! I have an array of IP adresses and I want to check if the corresponding servers are available. 我有一系列IP地址,我想检查相应的服务器是否可用。

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 您可以使用ajax complete事件来检查是否所有链接都经过测试

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? 执行此操作的唯一方法是使用这样的hackish解决方案: 是否可以从Javascript ping服务器?

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. 由于您希望保存结果,我将假设您使用的是服务器端语言,如PHP或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. 我熟悉PHP所以我建议让PHP执行PING命令,并根据该结果判断服务器是否在线。

EDIT 编辑

In regards to the first comment I would recommend implementing an httprequest instead of PING. 关于第一条评论,我建议实施一个httprequest而不是PING。

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

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