40

I use jQuery to make an AJAX POST request to my server, which can return HTTP response with status 302. Then JavaScript just sends GET request to this URL, while I'd like to redirect user to URL in this response. Is this possible?

Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
Alexander Solovyov
  • 1,526
  • 1
  • 13
  • 21

5 Answers5

23

The accepted answer does not work for the reasons given. I posted a comment with a link to a question that described a hack to get round the problem of the 302 being transparently handled by the browser:

How to manage a redirect request after a jQuery Ajax call

However, it is a bit of a dirty hack and after much digging around I found what I think is a better solution - use JSON. In this case, you can make all responses to ajax requests have the code 200 and, in the body of the response, you add some sort of JSON object which your ajax response handler can then use in the appropriate manner.

Community
  • 1
  • 1
Steg
  • 10,000
  • 3
  • 24
  • 17
  • 15
    Not much applicable in many cases [if you have a framework handling authorization] – naugtur Apr 12 '12 at 11:29
  • 1
    @Steg I don't think it's a good solution. If you have many ajax calls, do you have to add the checking logic to every ajax function? – smwikipedia Jan 06 '16 at 13:24
  • another option is to generate an error but add a custom header in the server response that your application can use to decide wether to change the location of the page that initiated the ajax call to whatever the Location specifies (or whatever else -- in my case I just refresh the page because I'm trying to catch issues where the user is not logged in, and refreshing the page lets my backend, django, use it's normal "login_required" mechanism to log the user in an return them to the page) – Kasapo Oct 27 '16 at 21:54
6

I don't think so. The W3C says that HTTP redirects with certain status codes, including 302, must be transparently followed. Quoted below:

If the response is an HTTP redirect (status code 301, 302, 303 or 307), then it MUST be transparently followed (unless it violates security or infinite loop precautions). Any other error (including a 401) MUST cause the object to use that error page as the response.

As an experiment, I tried doing Ajax requests from various browsers (Firefox 3.5, Chrome, IE8, IE7, IE6) to a server giving a 302 status code, and showing the status in the browser's request object. In every case, it showed up as 200.

smwikipedia
  • 61,609
  • 92
  • 309
  • 482
Elias Zamaria
  • 96,623
  • 33
  • 114
  • 148
2

In my problem reason was:

i was using localhost/Home/Test addres for testing the page. But ajax request code using 127.0.0.1/Home/AjaxRequest for url parameter. When the urls are different this error occurs.

maybe it helps someone :)

Orhaan
  • 342
  • 1
  • 4
  • 14
-1

Rather than asking the Javascript code to Handle 302, it would be better to return a 500 with custom error code+ message on event of 302

rjha94
  • 4,292
  • 3
  • 30
  • 37
-5
function doAjaxCall() {
   $.ajaxSetup({complete: onRequestCompleted});
   $.get(yourUrl,yourData,yourCallback);
}

function onRequestCompleted(xhr,textStatus) {
   if (xhr.status == 302) {
      location.href = xhr.getResponseHeader("Location");
   }
}
Strelok
  • 50,229
  • 9
  • 102
  • 115
  • 4
    at least in FF this does not work as the complete handler is called with the final HTTP status which is 200 for a successfull redirect (even though firebug correctly shows the 302 response followed by the 200 GET request). – o_o Sep 18 '09 at 16:51
  • 1
    Check out the post http://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery-ajax-call. This explains a bit of a hack to get around the browser automatically handling the 302 for ajax response. – Steg Oct 01 '09 at 23:25
  • 3
    This does not catch 302 response. – h--n Jan 01 '12 at 20:19