0

I need to check whether a cross-domain server is available or not. Choices are:
- sending ping
- getting a HEAD request
- making own server as proxy server and pass cross-domain server data in arguments
- making a JSONP request

  • I want to do this in client side (I can always reach cross-domain server from my server).
  • I need to know client's state. So javascript is mandatory.
  • I don't want to use own server as proxy, because this can cause security problems and I can't know client's state.
  • I tried JSONP below, but it is also OK to have a ping solution or any other solution that you can recommend.
  • I don't have any privilege in cross-domain server. I can only make restful requests from server with api key.
  • I prefer asynced, jquery based solutions. But it is ok to use pure javascript.
  • The only thing I need is to check whether server is available or not. It would be better if I can know the response time from server. I don't need other data about server.

My fiddle is like this.

$.ajax({
    url: "https://twitter.com",
    dataType: 'jsonp',
    success: function (data) {
        console.log("success");
        //console.log("data2:"+data);
        //console.log("data:"+JSON.stringify(data));
    },
    error: function (r, s, e) {
        console.log("error status: " + r.statusText + ", " + r.status);
        console.log("error r: " + r);
        console.log("error e: " + e);
    }
});

When I try this code I get "Uncaught SyntaxError: Unexpected token < " error in first line of the response data. So consequent javascript codes doesn't work.

How can I check this availability ?

trante
  • 30,976
  • 45
  • 174
  • 260
  • Is is fair to say if a connection is made the server is available or are you looking for a specific value from ajax call? @patric-evans - thanks, I clicked the wrong button in haste. – HPWD Mar 29 '14 at 21:03
  • The only thing I need is to check whether server is available or not, in client's machine. (I can reach from my server) – trante Mar 30 '14 at 09:00
  • Do you have control of the remote server? A JSONP call needs a script at the other end that will wrap the JSON data in a callback function, suitable to be embedded in a ` – iain_simpson Mar 30 '14 at 09:49
  • @iain_simpson Thank you. I edited question. I don't have control on remote server. I can only make restful requests. – trante Mar 30 '14 at 09:59
  • Server-side call is the way to go then, I'm afraid. You'll not be able to query Twitter directly from the browser - you need a proxy script. – iain_simpson Mar 30 '14 at 10:02
  • Twitter's Javascript API is able to query the REST API because its script is loaded from that domain. – iain_simpson Mar 30 '14 at 10:06
  • [You cannot send an actual PING from js](http://stackoverflow.com/q/4282151/1048572) – Bergi Mar 30 '14 at 13:39

0 Answers0