tags:

views:

answers:

Im using $.post() to call a Servlet using Ajax and then use the resulting HTML fragment to replace a div element in the User's current page. However, if the session timeouts the server sends a redirect directive to send the user to the login page. Nonetheless, JQuery is replacing the div element with the contents of the login page, forcing the user's eyes to witness a rare scene indeed.

How can I manage a redirect directive from an Ajax call?

jQuery 1.2.6

+3 A:

You can handle the AJAX response code to see whether it is a "200" or not (redirect is a 3xx) code

I was trying to avoid the low level stuff. Anyway, suppose I use something like what you describe, how do I force a browser redirection once I detect that the HTTP code is 3xx? My goal is to redirect the user, not just to announce that his/her session has expired.

Oh boy! Sorry I had to "un-accept" your answer, it still very helpful. Thing is, redirections are automatically managed by the XMLHttpRequest, hence I ALWAYS get a 200 status code after the redirection (sigh!). I think I will have to do something nasty like parsing the HTML and look for a marker.

I corrected the code. Thanks. Sorry that I couldn't be of more help. By the way, this is one of the reasons why people implement a response format (e.g. XML or JSON) which contains a more verbose status etc.. Look into that maybe. Or let me know, I can post an example if you need.

in the servlet you should put
response.setStatus(response.SC_MOVED_PERMANENTLY);
to send the '301' xmlHttp status you need for a redirection...

and in the $.ajax function you should not use the .toString() function..., just

if (xmlHttp.status == 301) {
top.location.href = 'xxxx.jsp';
}

the problem is it is not very flexible, you can't decide where you want to redirect..

redirecting through the servlets should be the best way. but i still can not find the right way to do it.

2008-11-14 20:56:26

+9 A:

No browsers handles 301 and 302 responses correctly. And in fact the standard even says they should handle them "transparently" which is a MASSIVE headache for Ajax Library vendors. In Ra-Ajax we were forced into using HTTP response status code 278 (just some "unused" success code) to handle transparently redirects from the server...

This really annoys me, and if someone here have some "pull" in W3C I would appreciate that you could let W3C know that we really need to handle 301 and 302 codes ourselves...! ;)

The solution that was eventually implemented was to use a wrapper for the callback function of the Ajax call and in this wrapper check for the existence of a specific element on the returned HTML chunk. If the element was found then the wrapper executed a redirection. If not, the wrapper forwarded the call to the actual callback function.

What an awesome solution. I like the idea of a one-stop solution. I need to check for a 403 status, but I can use the ajaxSuccess bind on body for this (which is what I was really looking for.) Thanks.

I just did this and found that I needed ajaxComplete where I was using the $.get() function and any status other than 200 was not firing. In fact, I could have probably just bound to ajaxError instead. See my answer below for more details.

Based on my brief testing of Firefox, Safari, Opera, IE6/7, it seems the XMLHttpRequest.status does not return the same values and its not compatible across different browsers. I haven't found a more elegant solution.

I read this question and implemented the approach that has been stated regarding setting the response status code to 278 in order to avoid the browser transparently handling the redirects. Even though this worked, I was a little dissatisfied as it is a bit of a hack.

After more digging around, I ditched this approach and used JSON. In this case, all responses to ajax requests have the status code 200 and the body of the response contains a JSON object that is constructed on the server. The javascript on the client can then use the JSON object to decide what it needs to do.

I had a similar problem to yours. I perform an ajax request that has 2 possible responses: one that redirects the browser to a new page and one that replaces an existing HTML form on the current page with a new one. The jquery code to do this looks something like:

I wanted to specifically check for certain http status codes to base my decision on. However, you can just bind to ajaxError to get anything other than success (200 only perhaps?) I could have just written: