views:

2552

answers:

7
url = "http://example.com"
new Ajax.Request(url, {
  onComplete: function(transport) {
    alert(transport.status);
  }
});

I'd like that to return a status of 200 if the site is working, or 500 if it is not working, etc.. But that code is returning 0 all the time.

Ultimately, I want to have a setinterval function that regularly pings a website for uptime status.

+1  A: 

Prototype has onXYZ callbacks, for example:

new Ajax.Request(url, {
  method: 'get',
  on500: function(transport) {
    alert("failed!");
  },
  onSuccess: function(transport) { 
    alert("success!");
  }
});

On thing though, if the website is down (as in, not reachable), it will not return a 500 error, so your approach isn't very good for starters.

Sinan Taifour
A: 

I'd like to know the answer to this too. Is it because of the Same origin policy that it returns 0?

+5  A: 

With JQuery you should get your status with a code similar to what you have, and it will be something like this:

$.ajax({
  url: 'some.url.com',
  type: 'POST',
  complete: function(transport) {
     if(transport.status == 200) {
         alert('Success');
     } else {
         alert('Failed');
     }
  }
 });

And if you want to use prototype your code should only add this:

      onXYZ:function(transport){
      }

In the text above XYZ should be replaced by the http status code you want to catch for the response.

Hope this helps

chermosillo
It seems like the downside to the onXYZ approach is you have to write one out for every possible status you might get... 200, 301, 302, 400, 401, 404, 500... and those are just really common ones... the list goes on...
Gabriel Hurley
In your jQuery code the transport.status always returns 0 not the actual status number as expected for remote domain requests. It'll return 200 if it's on the same domain but if you try to go across domains you get a 0.
RedWolves
A: 

You should be able to get any arbitrary header with the getResponseHeader() method.

For jQuery you might try:

complete: function(transport, textstatus){ 
    alert(transport.getResponseHeader("Status"))
}

warning: not tested

Gabriel Hurley
Null or blank are the values I get not 200 or 500 etc.
RedWolves
+4  A: 

Ajax libraries don't "return status codes" themselves; that code is the HTTP response code returned in the response from the server. A status code of 200 indicates success; 404 indicates "not found", etc.

It's probable that a response code of 0 means the request wasn't even attempted. Is the request URL under the same domain (subdomain, if applicable) as that which the page is coming from? If not, then you may be running into problems with the same-origin policy, which prevents scripts from creating arbitrary requests.

To work around this, you'll need to proxy the data on the server side; for example using a script/framework handler/whatever which executes the web request and passes the data back down to clients. Call the "local" proxy instead of the remote data source.

Rob
+2  A: 

I was able to make remote domains work by creating a server side proxy file to pass the status code through. I used the code on this post to create a asp.net page that would just set the status code of the page to the web request status code.

I then used the ajax example that Chermosillo provided like so.

$.ajax({
  url: 'URLTestProxy.aspx?url=http://some.url.com',
  type: 'POST',
  complete: function(transport) {
     if(transport.status == 200) {
         alert('Success');
     } else {
         alert('Failed');
     }
  }
 });

This way here you can get around the same origin policy and still get the status code of the remote url.

RedWolves
If you just wanted to get the status code, I think that type: 'HEAD' might be a little bit more streamlined. Although maybe that's something for the .Net side, I'm not sure if jQuery supports HEAD or not.
travis
+1  A: 

It may be helpful to know that the transport param in the example is an XMLHttpRequest object. Full details of the methods and properties available from this object can be found here:

http://www.w3.org/TR/XMLHttpRequest/#the-xmlhttprequest-interface

Most notable in the context of this question are the response items:

  readonly attribute unsigned short status;
  readonly attribute DOMString statusText;
  DOMString getResponseHeader(DOMString header);
  DOMString getAllResponseHeaders();
  readonly attribute DOMString responseText;
  readonly attribute Document responseXML;
Earl Jenkins