捕获 javascript XMLHttpRequest 的重定向位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4465547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 12:32:19  来源:igfitidea点击:

Capture redirect location of javascript XMLHttpRequest

javascriptjqueryxmlhttprequest

提问by Snea

I know that you can't, when using an XMLHttpRequest, intercept a redirect or prevent it, as the browser will transparently follow it, but is it possible to either

我知道在使用 XMLHttpRequest 时,您不能拦截重定向或阻止它,因为浏览器将透明地跟随它,但是是否有可能

A. Determine whether a request redirected, or

A. 确定请求是否重定向,或

B. Determine whereit redirected to? (assuming that the response gives no hints)

B.确定在那里它重定向到?(假设响应没有给出提示)

Example code:

示例代码:

$.post("/my-url-that-redirects/", {}, 
    function(response, statusCode, xmlHttpRequest){
        //Somehow grab the location it redirected to
    }
);

In my case, firebug will first show a POST to the url, then a GET to the redirected url. Can that GET location be captured?

在我的情况下,firebug 将首先向 url 显示 POST,然后向重定向的 url 显示 GET。可以捕获该 GET 位置吗?

采纳答案by Vojta

1) Use different status code than 301 (2**) (if request by ajax) and handle redirection on client side:

1) 使用与 301 (2**) 不同的状态代码(如果由 ajax 请求)并在客户端处理重定向:

var STATUS = {
  REDIRECT: 280
};

$.post('/redirected', {}, function(response, status, request) {
  if (status == STATUS.REDIRECT) {
    // you need to return the redirect url
    location.href = response.redirectUrl;
  } else {
    $('#content').html(request.responseText);
  }
});

2) DO NOT REDIRECT:

2)不要重定向:

I use that in "redirect pattern" = redirecting after post request (you don't want to allow user to refresh the post request, etc..)

我在“重定向模式”中使用它 = 在发布请求后重定向(您不想让用户刷新发布请求等。)

With ajax request, this is not necessary, so when the post request is ajax, I do forward instead (just forward to different controller - depends on your server-side framework, or what you are using...). POST requests are not cached by browsers.

对于 ajax 请求,这不是必需的,所以当 post 请求是 ajax 时,我会改为转发(只是转发到不同的控制器 - 取决于您的服务器端框架,或者您正在使用什么......)。POST 请求不会被浏览器缓存。

Actually, I don't know what's the reason you need that, so this might not be so useful for you. This is helpful when server returns different responses for ajax requests than common requests, because when browser redirect ajax request, the redirected request is not XMLHttpRequest...

实际上,我不知道你需要它的原因是什么,所以这对你来说可能不是那么有用。这在服务器对 ajax 请求返回的响应与普通请求不同时很有用,因为当浏览器重定向 ajax 请求时,重定向的请求不是 XMLHttpRequest...

[updated]

[更新]

You can access headers (of redirected request) like that:

您可以像这样访问(重定向请求的)标头:

$.post('redirected', {}, function(r, s, req) {
  req.getAllResponseHeaders();
  req.getResponseHeader('Location');
});

There should be 'Location' header, but it depends on the server, which headers are sent back...

应该有“位置”标头,但这取决于服务器,哪些标头被发回......

回答by Ali

After 4 years now it's possible to find the last redirect location using responseURL from XHR instance in Chrome 42+ (Opera 29+) and Firefox 39+ but it's not available in IE, Edge or safari yet.

4 年后,现在可以在 Chrome 42+(Opera 29+)和 Firefox 39+ 中使用来自 XHR 实例的 responseURL 找到最后一个重定向位置,但它在 IE、Edge 或 safari 中尚不可用。