14

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".

kaiz.net
  • 1,984
  • 3
  • 23
  • 31
BenWillkommen
  • 1,472
  • 13
  • 21
  • You might want to read this post: http://stackoverflow.com/a/1534662/64741 – Zachary Apr 30 '12 at 22:37
  • possible duplicate of [Determining what jQuery .ajax() resolves a string of redirects to](http://stackoverflow.com/questions/6767618/determining-what-jquery-ajax-resolves-a-string-of-redirects-to) – JAAulde May 01 '12 at 10:57

3 Answers3

1

It's meant to work transparently, at least that's what's suggested here:

Ajax Redirection Handling and Prevent redirection of Xmlhttprequest

One thing you could do is pass the URL as a context parameter to the AJAX call, and then in the success compare the response URL to the URL property of the context object.

See here for information about the context: http://api.jquery.com/jQuery.ajax/#jQuery-ajax-settings

Update:

The real tricky one is the new URL, I think you can get it by calling this.url if you don't override th econtext.

for (var i = 0; i < arrayOfLegacyUrls.length; i++)
{
    $.ajax({
        url: arrayOfLegacyUrls[i], 
        success: function(response, status, jqxhr){
            var newUrl = jqxhr.getResponseHeader("X-MYAPP-PATH");
            writeToFileForBusinessUser(arrayOfLegacyUrls[i], newUrl);
        }
    });
}
Community
  • 1
  • 1
Meligy
  • 35,654
  • 11
  • 85
  • 109
  • When you say "response URL", do you mean the URL that my request is ultimately redirected to? How can I access that? – BenWillkommen May 01 '12 at 02:13
  • Yeah, I got confused thinking more about original URL. Try `this.url` inside the success method. I have updated the answer accordingly. – Meligy May 01 '12 at 02:39
  • Unfortunately, this.url contains the legacy URL that I already know. – BenWillkommen May 01 '12 at 13:55
  • Sorry for late update. Try `jqxhr.getResponseHeader("X-MYAPP-PATH")` instead and let me know how it goes. – Meligy May 02 '12 at 23:02
  • Sorry for the *really* late update. `jqxhr.getResponseHeader("X-MYAPP-PATH")` will only work if the server actually sends that header. If you have no control over what headers are returned, this won't work for you. – jordanbtucker Dec 18 '14 at 00:52
1

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.

Community
  • 1
  • 1
BenWillkommen
  • 1,472
  • 13
  • 21
  • The problem with this solution is that it only works if the response is HTML content (maybe XML too?). If you receive JSON, plain text, an image, nothing at all, etc., this won't work. – jordanbtucker Dec 18 '14 at 00:49
1

Check out XMLHttpRequest.responseURL

Josh
  • 17,834
  • 7
  • 50
  • 68