有什么方法可以在 jQuery 中检查服务器的可达性测试

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17877639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:08:10  来源:igfitidea点击:

Is there any way to check reachability test of server in jQuery

javascriptjquerycordovajquery-mobilecordova-2.0.0

提问by user2563256

Is there any way to check reachability test of server .If application not able to connect server then it show a alert? is there any thing method to check.. I check the internet connection .If there is no connection then i am showing a alert .But if there is an connection but there is no reachability of server than how can i handle this.? I am checking like this connection status..!!

有什么方法可以检查服务器的可达性测试。如果应用程序无法连接服务器,那么它会显示警报吗?有什么方法可以检查..我检查互联网连接。如果没有连接,那么我会显示警报。但如果有连接但服务器无法访问,我该如何处理。?我正在检查这样的连接状态..!!

setInterval(function () {
    connectionStatus = navigator.onLine ? 'online' : 'offline';
    if(connectionStatus=="offline"){
        // alert("There is no connection");
    }
}, 100);



$.ajax({url: "192.168.12.171",
        dataType: "jsonp",
        statusCode: {
            200: function (response) {
                alert('status 200');
            },
            404: function (response) {
                alert('status  404 ');
            }
        }                        
 });

回答by Gajotres

To test if your server is up you will need to connect to it and check status massage:

要测试您的服务器是否已启动,您需要连接到它并检查状态消息:

$.ajax('LINK GOES HERE', {
  statusCode: {
    404: function() {
      alert('Not working');
    },
    200: function() {
      alert('Working');
    }
  }
});

Working jsFiddle example: http://jsfiddle.net/Gajotres/PMrDn/47/

工作 jsFiddle 示例:http: //jsfiddle.net/Gajotres/PMrDn/47/

$.ajax({url: "http://api.themoviedb.org/2.1/Movie.search/en/json/23afca60ebf72f8d88cdcae2c4f31866/The Goonies",
        dataType: "jsonp",
        statusCode: {
            200: function (response) {
                alert('status 200');
            },
            404: function (response) {
                alert('status  404 ');
            }
        }                        
 });

EDIT :

编辑 :

Use this:

用这个:

$.ajax({url: "http://192.168.12.171",
        type: "HEAD",
        timeout:1000,
        statusCode: {
            200: function (response) {
                alert('Working!');
            },
            400: function (response) {
                alert('Not working!');
            },
            0: function (response) {
                alert('Not working!');
            }              
        }
 });

Working example: http://jsfiddle.net/Gajotres/PMrDn/48/

工作示例:http: //jsfiddle.net/Gajotres/PMrDn/48/

回答by Arne S

One hacky way that should work would be to load an image (or font, style, javascript) from the server and check whether or not that image loads. This method would of course only work as long as there is something to load from the server that is not restricted by CORS policies.

一种可行的方法是从服务器加载图像(或字体、样式、javascript)并检查该图像是否加载. 这种方法当然只适用于从服务器加载不受 CORS 策略限制的内容。