簡體   English   中英

jQuery來檢查服務器可用性

[英]jQuery to check server availability

我想為我的網站編寫一個函數,它檢查幾個Web服務器以查看它們是否在線...我正在使用jQuery.ajax()我意識到我必須使用JSONP,但我真的不知道怎么做! 我有一系列IP地址,我想檢查相應的服務器是否可用。

到目前為止,這是我的代碼:

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");

之后,我想保存網絡服務器是否在線,但在哪里?

您可以使用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是你的控制器+方法),這里做驗證狀態並插入你的數據庫

執行此操作的唯一方法是使用這樣的hackish解決方案: 是否可以從Javascript ping服務器?

由於您希望保存結果,我將假設您使用的是服務器端語言,如PHP或ASP。 我熟悉PHP所以我建議讓PHP執行PING命令,並根據該結果判斷服務器是否在線。

編輯

關於第一條評論,我建議實施一個httprequest而不是PING。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM