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

2

9

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 ');
            }
        }                        
 });
Fugue answered 26/7, 2013 at 9:24 Comment(1)
I don't think the selected answer is correct. It works only when status is 200.Hakenkreuz
N
23

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/

$.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/

Nitrous answered 26/7, 2013 at 9:30 Comment(7)
wait checking ..tell you after 5 min..!!Fugue
when i am using your code it show code 200 ok.But when i give my ip then i am facing problem .I am getting data from server .But alert say not connected..:(Fugue
I will write you a solutionNitrous
Error 400 expects server, even if it is unreachable, status 0 will be thrown if nothing existNitrous
let us continue this discussion in chatFugue
how do you hope the cross origin rule?Assault
Several solutions exist, but in this case, it's the easiest one, jsonp is used instead of json.Nitrous
T
0

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.

Towbin answered 25/1, 2018 at 9:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.