当对$ .ajax的响应为301时,我可以通过编程方式获取新的URL吗?

时间:2022-10-07 08:35:19

Is there a way to get the URL you are ultimately redirected to when the response to an xhr request is 301?

当对xhr请求的响应是301时,有没有办法获取最终重定向到的URL?

I have a site that contains numerous legacy URLs from an older version, which return 301 responses to the correct new URL.

我的网站包含来自旧版本的大量遗留网址,这些网址会返回301对正确新网址的响应。

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

出于实用目的,我希望能够向旧URL发出请求,并能够检索新的URL,即发送请求到“/oldpage.aspx?foo=someParam”,获取新的URL“/ arbitaryNewPageName / someParam”。

I've been playing around with this in the firebug console:

我在firebug控制台中一直在玩这个:

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

我可以在firebug中看到,当这段代码运行时,它会对“/oldpage.aspx?foo=someParam”执行一次GET,并获得301响应,然后另一个GET为“/ 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.

对于第一个请求,我在响应标头的Location值中看到重定向的URL。不幸的是,第二个请求是传递给$ .ajax.success函数的,它只在请求头的Referrer值中有重定向的URL。

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:

业务用户要求我创建一个将旧URL与新URL相关联的列表。由于我已经实现了将旧URL重定向到服务器上的新URL的方法,因此我希望能够减少这项工作,并创建一个脚本,将请求放置到旧URL并获取请求被重定向到的URL。像这样的东西:

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

我的问题的关键在于:我可以从XHR获取我的请求被重定向到的URL吗?到目前为止,答案似乎是“不”。

3 个解决方案

#1


1  

I found a way to do this by using the actual XHR object instead of jquery's ajax method from this answer: https://*.com/a/7015798/194099

我找到了一种方法,通过使用实际的XHR对象而不是jquery的ajax方法来做到这一点:https://*.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.

有了这个,我能够向一个我知道将返回301的URL发出请求,并获取请求被重定向到的URL。

#2


1  

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

它意味着透明地工作,至少这是建议的:

Ajax Redirection Handling and Prevent redirection of Xmlhttprequest

Ajax重定向处理和防止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.

您可以做的一件事是将URL作为上下文参数传递给AJAX调用,然后在成功中将响应URL与上下文对象的URL属性进行比较。

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

有关上下文的信息,请参见此处: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.

真正棘手的是新的URL,我想你可以通过调用this.url得到它,如果你不覆盖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


1  

Check out XMLHttpRequest.responseURL

查看XMLHttpRequest.responseURL

#1


1  

I found a way to do this by using the actual XHR object instead of jquery's ajax method from this answer: https://*.com/a/7015798/194099

我找到了一种方法,通过使用实际的XHR对象而不是jquery的ajax方法来做到这一点:https://*.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.

有了这个,我能够向一个我知道将返回301的URL发出请求,并获取请求被重定向到的URL。

#2


1  

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

它意味着透明地工作,至少这是建议的:

Ajax Redirection Handling and Prevent redirection of Xmlhttprequest

Ajax重定向处理和防止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.

您可以做的一件事是将URL作为上下文参数传递给AJAX调用,然后在成功中将响应URL与上下文对象的URL属性进行比较。

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

有关上下文的信息,请参见此处: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.

真正棘手的是新的URL,我想你可以通过调用this.url得到它,如果你不覆盖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


1  

Check out XMLHttpRequest.responseURL

查看XMLHttpRequest.responseURL