Is there a way to get the URL you are ultimately redirected to when the response to an xhr request is 301?
I have a site that contains numerous legacy URLs from an older version, which return 301 responses to the correct new URL.
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".
I've been playing around with this in the firebug console:
$.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);
}
});
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".
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.
Is there perhaps a way to intercept the response to the first response, or maybe see the request headers for the second request?
Edit: Thanks everyone for the responses. I think I need to give a little background to clarify what exactly I'm looking for.
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:
for (var i = 0; i < 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);
}
});
}
The crux of my question is this: Can I get the URL my request was redirected to from the XHR? So far, the answer seems to be "no".
I found a way to do this by using the actual XHR object instead of jquery's ajax method from this answer: https://stackoverflow.com/a/7015798/194099
var r = new XMLHttpRequest();
r.open("GET", "http://mysite.com/legacyUrl.aspx?bla=bla");
r.overrideMimeType("text/xml");
r.onload = function()
{
alert(r.responseXML.baseURI); //gets the URL the request was redirected to! huzzah!
}
r.send(null);
With this, I was able to place a request to a URL I know will return a 301, and get the URL that the request is being redirected to.