How to get response url in XMLHttpRequest?

25,111

The final URL (after following all redirects) is available in the responseURL attribute of a XMLHttpRequest instance. This feature is new and only supported in Firefox 32 stable in September 2014 [ref] and Chrome 37.0.2031.0 stable in August 2014 [ref] (and probably also Opera 24). responseURL is not (yet) supported in IE 11 or Safari 7 and older browsers. For these browsers, the previous answer is still true:

XMLHttpRequest automatically follows redirects, without saving the served URLs in a property. The Location header can neither be retrieved through the .getResponseHeader().

References:

Share:
25,111
Okan Kocyigit
Author by

Okan Kocyigit

https://bitbucket.org/ocanal/ https://github.com/okankocyigit Email: hasanokan[at]gmail[dot]com

Updated on June 20, 2020

Comments

  • Okan Kocyigit
    Okan Kocyigit about 4 years

    There is a page (url), I request it by XMLHttpRequest, but I'm not getting response from requested url, It's directing request to another page,

    requesting --- > page.php
    getting reponse from > directedpage.php

    and the question is how can I get response url? (directedpage.php in example)

  • Julien Palard
    Julien Palard about 11 years
    OK, but, does someone know why ? Security ? Which attack should be available with the response url ? (Giving response url only if response url is in the same origin than the original url)
  • Rob W
    Rob W about 11 years
    @JulienPalard The specification demands that redirects have to be followed transparently.
  • user2284570
    user2284570 almost 9 years
    @RobW : I don’t understand, if I have thisA=302—B=302—>C=302—>A=200. What responseURL will return ? The ᴜʀʟ of C or A ?
  • Rob W
    Rob W almost 9 years
    @user2284570 The URL of the last A.
  • user2284570
    user2284570 almost 9 years
    @RobW : And in the case of a circled redirect ?
  • Rob W
    Rob W almost 9 years
    @user2284570 It will probably be null, because the browser will detect the redirect loop and abort the request.
  • traxium
    traxium over 7 years
    Is it impossible to get responseURL for blocked CORS requests?
  • Rob W
    Rob W over 7 years
    @traxium It is not possible to get responseURL for blocked cross-origin requests. If the server did not respond with the expected CORS response headers. then the browser has to assume that the server is not willing to volunteer sensitive information and therefore it will not leak the redirection target to the script.