PDA

View Full Version : AJAX Check if server is down.


MikoLone
03-26-2008, 07:43 PM
How can you check with ajax if the server is down. I hacked my way into a solution but I imagine there is a better way. This is how I did it. The snippet is inside the onreadystatechange event.


try{
var blah = GlobalRequester.status;
}catch (e){
alert('servers down');
GlobalRequester = null;
return;
}


I noticed that when the server is down the httprequester.status throws an error so I catch and then alert server error. This works but I imagine there are other times when the status will throw an error like this and I am just wondering if there is another way to know that the server is down.

Thanks

Michael

MikoLone
03-26-2008, 11:57 PM
The answer is that you never get a readyState 3 if your server is down.

So I would set a flag when I get readyState 3 and then check if I got it when I get ready state 4. If I didn't get it then I know I have no data and I do some kind of error something (Send back error as the responseText or alert(server is down))

Anyway, I hope this helps for future reference if anyone else thinks that there server might go down while the users are using your ajax app.