Is there any way to check server availability check in jQuery

Is there any way to check server availability check. If the application cannot connect to the server, then does it show a warning? is there any way to check .. I am checking my internet connection. If there is no connection, then I show a warning. But if there is a connection, but there is no server inaccessibility, how can I handle this.? I check how 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 '); } } }); 
+4
source share
2 answers

To check if your server is working, you need to connect to it and check the massage status:

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

+19
source

One hacker way that should work is to download the image (or font, style, javascript) from the server and check if this image is loading. Of course, this method only works until something is downloaded from a server that is not limited to CORS policies.

0
source

Source: https://habr.com/ru/post/1493621/


All Articles