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

后端 未结 2 1280
眼角桃花
眼角桃花 2020-12-05 16:36

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 inter

相关标签:
2条回答
  • 2020-12-05 16:56

    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/

    0 讨论(0)
  • 2020-12-05 17:04

    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.

    0 讨论(0)
提交回复
热议问题