160

What happens if the browser receives a redirect response to an ajax request?

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Vasil
  • 36,468
  • 26
  • 90
  • 114

3 Answers3

236

What happens if the browser receives a redirect response to an ajax request?

If the server sends a redirect (aka a 302 response plus a Location: header) the redirect is automatically followed by the browser. The response to the second request (assuming it also isn't another redirect) is what is exposed to your program.

In fact, you don't have the ability to detect whether a 302 response has occurred. If the 302 redirect leads to a 200, then your program acts identically as if the original request led directly to a 200.

This has been both my experience and the behavior called out in the spec.

2016 Update: Time has passed, and the good news is that the new fetch() API is spec'd to offer finer-grained control of how redirects are handled, with default behavior similar to XHR. That said, it only works where fetch() is implemented natively. Polyfill versions of fetch()—which are based on XHR—continue to have XHR's limitations. Fortunately, native browser support seems to be rounding out nicely.

greim
  • 9,149
  • 6
  • 34
  • 35
  • 61
    Interestingly, I arrived at this because I am experiencing a situation where the redirect is apparently not being followed... this happens when the redirect violates same origin policy. – Gus May 22 '13 at 00:32
  • 5
    @Gus which is probably logical – Dmitry Nov 22 '13 at 19:43
  • What is supposed to happen if the redirect leads to a 401 instead of a 200? I seem to be running into that issue and getting errors with jQuery, so I am trying to figure out exactly what the brower/JS is supposed to be doing. – Eric B. Jan 14 '14 at 18:27
  • 1
    In the case of a redirect to a 401 (or any 4xx or 5xx error) I'd assume your program would behave as if the request led directly to a 401. Is that not what you're seeing? – greim Jan 15 '14 at 22:16
  • 2
    A RESTful API may send 201 and a Location header after a POST request; see http://www.restapitutorial.com/lessons/httpmethods.html. –  May 25 '15 at 11:35
  • What about headers? To they follow the redirection? Can I define this behavior? – Augustin Riedinger Jul 29 '15 at 17:43
  • If requestA redirects to requestB, then your code will only be able to see headers belonging to requestB. – greim Jul 29 '15 at 17:49
  • If it's any help: I know that my `window.location.pathname.split('/').length` should be the same length as the parsed and prepared **xhttp.responseURL**. So if it's not the same length, it is _probably_ a redirect – in my case. But thinking about it: if you know the URL and expect your response to be from the _same URL_, then you could just compare your expected URL with the _xhttp.responseURL_. – Sebastian G. Marinescu May 23 '16 at 19:13
  • for those who are using the fetch library by github, please note that follow redirect is not working. https://github.com/github/fetch/issues/137 – Yang Wei Sep 14 '16 at 09:22
  • 4
    2019 update: fetch doesn't work as we were expecting 3 years ago ): – lcjury Nov 20 '19 at 00:59
7

The ajax-request will follow that redirect afaik. The actual content (.responseText, .responseXML) will be the content from the page you are redirected to.

You might be able to intercept the redirect (status-code, location-header) on readyState 2 or 3, but not sure about it.

shashwat
  • 7,851
  • 9
  • 57
  • 90
jishi
  • 24,126
  • 6
  • 49
  • 75
2

TLDR: it's doable with fetch.
More info here: https://github.com/axios/axios/issues/932#issuecomment-515229573

kissu
  • 40,416
  • 14
  • 65
  • 133