Note that there are some explanatory texts on larger screens.

plurals
  1. POWhen a response to $.ajax is 301, can I programmatically get the new URL?
    primarykey
    data
    text
    <p>Is there a way to get the URL you are ultimately redirected to when the response to an xhr request is 301?</p> <p>I have a site that contains numerous legacy URLs from an older version, which return 301 responses to the correct new URL.</p> <p>For utility purposes, I would like to be able to make a request to an old URL, and be able to retrieve the new one i.e. send request to "/oldpage.aspx?foo=someParam", get back the new url "/arbitaryNewPageName/someParam".</p> <p>I've been playing around with this in the firebug console:</p> <pre><code> $.ajax({ url: "/oldpage.aspx?foo=someParam", success: function(response, status, jqxhr){ //poking around, trying to get the new URL, "/arbitraryNewPage/someParam" console.log(jqxhr.getAllResponseHeaders()); console.log(jqxhr); }, beforeSend: function(jqxhr, settings){ console.log(jqxhr); console.log(settings); } }); </code></pre> <p>I can see in firebug that when this code runs, it does one GET to "/oldpage.aspx?foo=someParam", and gets a 301 response, then another GET to "/arbitaryNewPageName/someParam".</p> <p>For the first request, I see the redirected URL in the Location value of the response header. Unfortunately, the second request is what is passed to the $.ajax.success function, and it only has the redirected URL in the Referrer value of the request header.</p> <p>Is there perhaps a way to intercept the response to the first response, or maybe see the request headers for the second request?</p> <p><strong>Edit:</strong> Thanks everyone for the responses. I think I need to give a little background to clarify what exactly I'm looking for.</p> <p>A business user has asked me to create a list that associates legacy URLs with new URLs. Since I have already implemented a means of redirecting legacy URLs to new URLs on the server, I was hoping to piggy back off that work and create a script that placed requests to the legacy URLs and got the URLs that the requests were redirected to. Something like this:</p> <pre><code>for (var i = 0; i &lt; arrayOfLegacyUrls.length; i++) { $.ajax({ url: arrayOfLegacyUrls[i], success: function(response, status, jqxhr){ var newUrl = "???"; //do magic to get URL that request was redirected to writeToFileForBusinessUser(arrayOfLegacyUrls[i], newUrl); } }); } </code></pre> <p>The crux of my question is this: <strong>Can I get the URL my request was redirected to from the XHR?</strong> So far, the answer seems to be "no".</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload