I’m using the jquery.form plugin and its ajaxSubmit method with great success so far, but now I’m stuck with the way it handles HTTP 302. Let me explain:
- User submit a form through xhr using
ajaxSubmit - If the form is invalid, the response contains the HTML code of the form with errors and so on (HTTP 200), and that’s ok
- If the for is valid, the backend returns an HTTP 302, but here lies the problem…
ajaxSubmit seems to catch the 302 and do the redirect internally and fetches the redirected contents, so all I get in success or complete event callbacks are the HTTP 200 it retrieves after having followed the location provided by the redirect.
What I would like to do is to check for a 302 and then do something with the redirect location url (for example, redirect the browser window, not just updating part of the DOM of current page.)
Is it possible to achieve this? I feel a bit dumb atm because I’m sure it is :$
Any help much appreciated 🙂
Self answering here.
I stumbled upon this blog post where the guy uses Django and faces the same problem with HTTP redirect support in browser xmlHttpRequest implementations.
He came out with a very nice solution, handling specifically ajax request from the backend setting up a custom status code and test after it from the ajax event callbacks.
Hope this helps someone else.