While the other folks who answered this question are (sadly) correct that this information is hidden from us by the browser, I thought I'd post a workaround I came up with:
I configured my server app to set a custom response header (X-Response-Url
) containing the url that was requested. Whenever my ajax code receives a response, it checks if xhr.getResponseHeader("x-response-url")
is defined, in which case it compares it to the url that it originally requested via $.ajax()
. If the strings differ, I know there was a redirect, and additionally, what url we actually arrived at.
This does have the drawback of requiring some server-side help, and also may break down if the url gets munged (due to quoting/encoding issues etc) during the round trip... but for 99% of cases, this seems to get the job done.
On the server side, my specific case was a python application using the Pyramid web framework, and I used the following snippet:
import pyramid.events
@pyramid.events.subscriber(pyramid.events.NewResponse)
def set_response_header(event):
request = event.request
if request.is_xhr:
event.response.headers['X-Response-URL'] = request.url