ampersandre
ampersandre

Reputation: 3216

Doing an AJAX request to a website that might be up, or might be down

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?

Upvotes: 1

Views: 248

Answers (4)

EricLaw
EricLaw

Reputation: 57075

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.

Upvotes: 1

Amr Elgarhy
Amr Elgarhy

Reputation: 68902

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

Upvotes: 0

karim79
karim79

Reputation: 342625

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

Upvotes: 1

RiddlerDev
RiddlerDev

Reputation: 7439

You can check the header for a 404 error.

Upvotes: -1

Related Questions