When a response to $.ajax is 301, can I programmat

2019-03-18 09:27发布

问题:

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

回答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.



回答2:

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);
        }
    });
}


回答3:

Check out XMLHttpRequest.responseURL