0

I want to write a function for my website, which checks several web servers to see if they are online or not... I'm working with jQuery.ajax() I realized that I have to use JSONP, but i really dont know how to do that! I have an array of IP adresses and I want to check if the corresponding servers are available.

Here is my code so far:

function urlExists(url){

            $.ajax({
                timeout: 5000,
                type: 'GET',
                dataType: 'jsonp',
                url: "http://" + url,
                cache: false,
                "error":function(XMLHttpRequest,textStatus, errorThrown) {
                    if(errorThrown == "timeout") {
                        alert("woah, there we got a timeout...");
                        // At this point the request shall abort
                    }
                },
                statusCode: {
                    404:function(){
                        alert("404!");

                    },
                    0:function(){
                        alert("0!");

                    },
                    500:function(){
                        alert("500!");

                    },
                    200:function(){
                        alert("it worked!");

                    }
                }
            });
        }

        urlExists("192.168.5.55");

After that i want to save whether the webserver is online or not, but where?

RacerNerd
  • 1,551
  • 1
  • 13
  • 30
shinchillahh
  • 13
  • 1
  • 3
  • What's not working? If it returns `success`, there's where you know it's online. – melancia Oct 14 '13 at 13:40
  • 1
    You cannot do that without cooperation from the external site. You could check whether an image loads successfully. – SLaks Oct 14 '13 at 13:40
  • 2
    Why don't you write a server side function that is called from your jquery, so the server does all the heavy lifting. Attempting to do cross domain ajax calls from jquery is only going to bring you problems. – ORION Oct 14 '13 at 13:42
  • JSONP doesn't magically let you load arbitrary URLs with JavaScript. JSONP must be enabled on the website(s) you wish to load. Chances are, these sites aren't JSONP enabled, so this is not going to work. You should have your server-side code do this. – Rocket Hazmat Oct 14 '13 at 13:44

3 Answers3

2

You can use ajax complete event to check if all the links are tested or not

function urlExists(url){
    $.ajax({
        timeout: 5000,
        type: 'GET',
        dataType: 'jsonp',
        url: "http://" + url,
        cache: false,
        "error":function(XMLHttpRequest,textStatus, errorThrown) {
            if(errorThrown == "timeout") {
                result.push({
                'url': url,
                'status': "woah, there we got a timeout..."
            });
           // At this point the request shall abort
        }
    },
    complete: function(){
     // Handle the complete event
     if(result.length == list.length) {
            alert("All url's checked. Check the console for 'result' varialble");
        console.log(result);
        }
    },
    statusCode: {
        404:function(){
            result.push({
                'url': url,
                'status': "404!"
           });

        },
        0:function(){
            result.push({
                    'url': url,
                    'status': "0!"
                });
            },
            500:function(){
                result.push({
                    'url': url,
                    'status': "500"
               });
            },
            200:function(){
                result.push({
                    'url': url,
                    'status': "it worked!"
               });
            }
        }
    });
}
var result = new Array;
var list = ['192.168.5.55','192.168.98.99'];
for (var i = list.length - 1; i >= 0; i--) {
    urlExists(list[i]);
};
Bhavesh B
  • 1,121
  • 12
  • 24
0

You must call a php function where you have url: "http://" + url,(url is your controller+method) and here make verification status and insert in your database

BlackWhite
  • 808
  • 2
  • 12
  • 23
0

The only way to do this is involves a hackish solution like this: Is it possible to ping a server from Javascript?

Since you would like to save the results, I am going to assume you are using a server-side language such as PHP or ASP. I am familiar with PHP so I would recommend getting PHP to execute a PING command and figure out if the server is online or not based on that result.

EDIT

In regards to the first comment I would recommend implementing an httprequest instead of PING.

Community
  • 1
  • 1
MonkeyZeus
  • 18,445
  • 3
  • 30
  • 67