【问题标题】:Is there any way to check reachability test of server in jQuery有没有办法在 jQuery 中检查服务器的可达性测试
【发布时间】:2013-07-26 12:06:07
【问题描述】:

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

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 ');
            }
        }                        
 });

【问题讨论】:

  • 我认为所选答案不正确。仅当状态为 200 时才有效。

标签: javascript jquery cordova jquery-mobile cordova-2.0.0


【解决方案1】:

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

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

工作 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 ');
            }
        }                        
 });

编辑:

使用这个:

$.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!');
            }              
        }
 });

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

【讨论】:

  • 等待检查.. 5分钟后告诉你..!!
  • 当我使用你的代码时,它显示代码 200 ok。但是当我给我的 ip 时,我遇到了问题。我正在从服务器获取数据。但是警报说没有连接..:(跨度>
  • 我给你写一个解决方案
  • 错误 400 期望服务器,即使它无法访问,如果不存在将抛出状态 0
  • 你希望跨域规则怎么样?
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-02
    • 2014-05-31
    • 1970-01-01
    相关资源
    最近更新 更多