When a response to $.ajax is 301, can I programmatically get the new URL?
Asked Answered
M

3

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

Micaelamicah answered 30/4, 2012 at 22:29 Comment(2)
You might want to read this post: https://mcmap.net/q/45204/-how-to-manage-a-redirect-request-after-a-jquery-ajax-callLustrate
possible duplicate of Determining what jQuery .ajax() resolves a string of redirects toBumpy
M
1

I found a way to do this by using the actual XHR object instead of jquery's ajax method from this answer: https://mcmap.net/q/372749/-determining-what-jquery-ajax-resolves-a-string-of-redirects-to

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.

Micaelamicah answered 1/5, 2012 at 14:1 Comment(1)
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.Rawlings
E
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);
        }
    });
}
Enunciation answered 1/5, 2012 at 0:25 Comment(5)
When you say "response URL", do you mean the URL that my request is ultimately redirected to? How can I access that?Micaelamicah
Yeah, I got confused thinking more about original URL. Try this.url inside the success method. I have updated the answer accordingly.Enunciation
Unfortunately, this.url contains the legacy URL that I already know.Micaelamicah
Sorry for late update. Try jqxhr.getResponseHeader("X-MYAPP-PATH") instead and let me know how it goes.Enunciation
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.Rawlings
M
1

I found a way to do this by using the actual XHR object instead of jquery's ajax method from this answer: https://mcmap.net/q/372749/-determining-what-jquery-ajax-resolves-a-string-of-redirects-to

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.

Micaelamicah answered 1/5, 2012 at 14:1 Comment(1)
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.Rawlings
U
1

Check out XMLHttpRequest.responseURL

Ufo answered 3/7, 2014 at 0:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.