tags:

views:

82

answers:

5

I'm writing a small app that will be used to monitor the status of a few websites, mainly just to report which websites are online and which websites are offline.

I currently have this code as the onreadystatechange function:

if(xmlhttp.readyState == 4) {
 if (xmlhttp.status == 200) {
  element(id).innerHTML = "Online";
 }
 else {
  element(id).innerHTML = "Offline";
 }
}

It runs properly when the website is online, but it never reaches the 'else' block if I do an AJAX request on a website that is offline.. I'm thinking the request never reaches readyState 4 if the website is down?

Any suggestions for how to capture an AJAX request to an offline website?

A: 

We have a similar question which was asked few minutes back : Ping site and return result in PHP

Shoban
A: 

I just tested now using Fiddler, and it returned Status 502, Body 512

Amr ElGarhy
Yeah, but my else block should be catching anything that isn't a 200. For some reason it doesn't
amougeot
A 502 is generated by a proxy if the server is not reachable. If there's no proxy, there will be no 502.
EricLaw -MSFT-
A: 

You can check the header for a 404 error.

IPX Ares
You won't get a 404 from a downed server, since the server isn't up to return the 404.
EricLaw -MSFT-
ah good point.. was thinking page not server.
IPX Ares
+1  A: 

What you will want to do is abort the request after a specific amount of time, i.e. a timeout. Here's an article that should help:

Async Requests over an Unreliable Network

karim79
Yeah, I did some investigating, and when you do an AJAX request to a resource that is offline, the readyState never reaches 4, it stays at 1 until you abort the request.
amougeot
+1  A: 

IE8's XHR object supports an ontimeout event (http://msdn.microsoft.com/en-us/library/cc197061(VS.85).aspx)

ReadyState=4 means "loaded", which won't happen if the content doesn't load.

EricLaw -MSFT-