Search code examples
javascriptweb-servicesjscript

Detect web service accessibility using JScript


Is there a common way to detect whether a web service is accessible, through javascript? For example, if the web server is down or IIS on it is stopped, I can stop the program.


Solution

  • You can simply use jQuery's AJAX function to send a request to the server, and listen for errors (i.e.: timeout)

    var request = $.ajax({
      url: "http://somewebsite.com",
      type: "POST"
    });
    
    request.done(function(msg) {
      $("#log").html("Website is up.");
    });
    
    request.fail(function(jqXHR, status) {
      $("#log").html("Website is down.");
    });