Can Prototype or JQuery return an HTTP status code on an AJAX request

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.

Answers:

Answer

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

Answer

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.

Answer

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.

Answer

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.

Answer

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;
Answer

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

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.