Hi,
I have this following code:

$('#content-load-target').load(location,function(response, status, xhr) {                   
    if (xhr.status != 200) {
        location.href = '/';
    } else {
        //do something else
    }
});

The response can be either an ajax page or be redirected (if the session has timed out). In case of redirect I want to reload the whole page. The problem is - the response code is always 200. Even if it redirects, it just loads the redirect page and returns 200. How do I properly catch redirects with this?

Thanks

Recommended Answers

@Buppy

The problem is - the response code is always 200.

It's either 0 or 200. There's only 2 options.

Read this:

http://www.w3schools.com/ajax/ajax_xmlhttprequest_onreadystatechange.asp

and this:

http://www.pearweb.com/javascript/XMLHttpRequest.html

Jump to Post

All 2 Replies

I havent had a need to implement this myself, but you should be able to check for other status codes, 302 for instance. In addition, for this to work, you have to make sure that the redirect does not violate security (it is same origin).

Also, in your case, I beleive that the correct operator that you should be using is "!==". In your case every response may result in false because of the operator.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.