有没有办法在jQuery中检查服务器的可达性测试

use*_*256 8 javascript jquery jquery-mobile cordova cordova-2.0.0

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

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 ');
            }
        }                        
 });
Run Code Online (Sandbox Code Playgroud)

Gaj*_*res 22

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

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

工作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 ');
            }
        }                        
 });
Run Code Online (Sandbox Code Playgroud)

编辑:

用这个:

$.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!');
            }              
        }
 });
Run Code Online (Sandbox Code Playgroud)

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

  • 你怎么希望交叉起源规则? (2认同)