no callbacks are called in Firefox after ajax request with jquery with 302 redirect
I have a Rails application that uses ajax extensively. With jQuery. For example one of the models is entirely managed with jQuery: records are created, updated and deleted ajaxly. Evertything works great except for updating records.
I did some testing and what I figured is that jQuery in Firefox (3.6 on Mac, 3.5 on Windows is what I've tested) has problem with detecting server's response when it's 302 redirected to the same URL开发者_Python百科. What I get in Firebug is:
POST localhost:3000/resources/1 -> 302 Found
GET localhost:3000/resources/1 -> 200 OK
And no jQuery's ajax callback is called. Neither success, complete nor error. But when I create a record and server redirects to another URL "success" callback is called:
POST localhost:3000/resources -> 302 Found
GET localhost:3000/resources/1 -> 200 OK
It doesn't matter if I call $.ajax myself or if I use jquery.form's ajaxForm().
Any ideas?
The 301/302/303 redirect are done by the XmlHttpRequest handler of the browsers, not by jQuery. This does not explain why you do not have the success callback. But it may explain why the redirection are not well tested on ajax side.
One problem is that some browser do follow the 302, and for example not the 301 or 303 redirect. In an old application we used the 303 see-other response code and handle it in the error handler. This was nice as we could do the new GET request instead of the browser (which were buggy and should have done it).
But we had problems, depending on browsers (I think, not sure).
When I see the 302 response behaviour you have I think you should do what we've done: remove the redirect after post handling for ajax requests. Send in the response either the final HTML you want (that would be a redirect server-side), or build your own protocol (based on JSON?) where you send your protocol status in a json key, and maybe a new url to check for a new response in another key, etc.
精彩评论