javascript XHR HEAD 请求是否可能不遵循重定向 (301 302)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3820663/
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-10-25 02:22:53  来源:igfitidea点击:

Is it possible for XHR HEAD requests to not follow redirects (301 302)

javascriptajaxgoogle-chromexmlhttprequest

提问by antonj

Is it possible to send an xhr HTTP HEAD request to only get header response for the first request and not automatically follow 301, 302 like redirects? I'm only interested in getting the new location of the url. Example:

是否可以发送 xhr HTTP HEAD 请求以仅获取第一个请求的标头响应,而不像重定向一样自动跟随 301、302?我只对获取 url 的新位置感兴趣。例子:

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(data) {
    if (xhr.readyState == 4) {
        if (xhr.status == 301 || xhr.status == 302) {
            // Get new location url don't GET it
        }
    }
};
xhr.open('HEAD', url, true);
xhr.send();

http://www.w3.org/TR/XMLHttpRequest/#infrastructure-for-the-send-methodseems to specify that requests should be followed, is there a way to stop this?

http://www.w3.org/TR/XMLHttpRequest/#infrastructure-for-the-send-method似乎指定应遵循请求,有没有办法阻止这种情况?

采纳答案by Nick Craver

There isn't, this isn't exposed behavior you can stop.

没有,这不是您可以阻止的暴露行为。

It's because of the spec you linked already, the specified behavior is that XmlHttpRequest should transparentlyfollow redirects...under the covers unfortunately, and not in a way you can prevent.

这是因为你已经链接的规范,指定的行为是 XmlHttpRequest 应该透明地遵循重定向......不幸的是,在幕后,而不是你可以阻止的方式。

It's this way to try and make things easier, if resources move, etc...but when it was designed and the spec laid out, all these redirection services weren't out there. There just wasn't a strong need for any other behavior or ability to prevent it, I think with as many redirects hitting the web not we'll see the ability added, but who knows when every browser would support it.

这是尝试使事情变得更容易的方式,如果资源移动等等......但是当它被设计和规范布局时,所有这些重定向服务都没有出现。只是没有强烈需要任何其他行为或能力来阻止它,我认为随着大量重定向访问网络,我们不会看到添加的能力,但谁知道每个浏览器何时会支持它。

回答by Daniel Vassallo

The W3C specification requires that redirects are followed automatically, as @Nick suggested in the other answer. However a property to disable redirects is being considered by the W3C for a future version of this specification:

W3C 规范要求自动遵循重定向,正如@Nick 在另一个答案中所建议的那样。然而,W3C正在考虑在本规范的未来版本中禁用重定向的属性:

This specification does not include the following features which are being considered for a future version of this specification:

  • load event and onload attribute;
  • error event and onerror attribute;
  • progress event and onprogress attribute;
  • abort event and onabort attribute;
  • Timers have been suggested, perhaps an ontimeout attribute;
  • Property to disable following redirects;
  • responseXML for text/html documents;
  • Cross-site XMLHttpRequest;
  • responseBody to deal with byte streams;
  • overrideMimeType to fix up MIME types;
  • getRequestHeader() and removeRequestHeader().

本规范不包括正在考虑用于本规范未来版本的以下功能:

  • 加载事件和 onload 属性;
  • 错误事件和 onerror 属性;
  • progress 事件和 onprogress 属性;
  • abort 事件和 onabort 属性;
  • 已建议使用计时器,可能是 ontimeout 属性;
  • 禁用以下重定向的属性;
  • 文本/html 文档的 responseXML;
  • 跨站 XMLHttpRequest;
  • responseBody 处理字节流;
  • overrideMimeType 修复 MIME 类型;
  • getRequestHeader() 和 removeRequestHeader()。

However, I wouldn't hold my breath until this is implemented by all browsers. You may want to use a server-side proxy to handle this. Simply write a short script to do a HEADrequest in your favourite server-side language/framework, and then use Ajax to query this service. You would also be able to do requests to third-party domains through the proxy as a positive side-effect.

但是,在所有浏览器都实现之前,我不会屏住呼吸。您可能希望使用服务器端代理来处理此问题。只需编写一个简短的脚本,HEAD用你喜欢的服务器端语言/框架来做一个请求,然后使用 Ajax 来查询这个服务。作为积极的副作用,您还可以通过代理向第三方域发出请求。

回答by hi_artem

And now it is possible! at least in Firefox and Chrome browsers

现在有可能!至少在 Firefox 和 Chrome 浏览器中

I was almost desperate, when I found that answer: https://stackoverflow.com/a/8056313/995007

当我找到答案时,我几乎绝望了:https: //stackoverflow.com/a/8056313/995007

Ex:

前任:

    var xhttp = new XMLHttpRequest();
    xhttp.onload = function() {
        console.log(xhttp.responseURL); // Here you are!
    };
    xhttp.open('GET', 'https://www.ddd.com/news', true);
    xhttp.send();

回答by Alix

Stop the redirection is POSSIBLE.

停止重定向是POSSIBLE

Major browsers will stop redirect when there are too many redirections. In chrome you will got an error ERR_TOO_MANY_REDIRECTS after 20 times redirect. Test code like this (save it as redir.php):

当重定向过多时,主流浏览器将停止重定向。在 Chrome 中,重定向 20 次后,您将收到错误 ERR_TOO_MANY_REDIRECTS。像这样测试代码(另存为 redir.php):

<?php
$times = isset($_GET['t']) ? intval($_GET['t']) : 1;
if ( $times < 20) {
    header("Location: redir.php?t=".strval($times+1));
} else {
    header("Location: http://the.final.target.url/which.will.give.you.a.301.header.but.you.do.not.want.to.follow.it");
}

Then you will find the redirection will stopped as we excepted. Also works with xhr and common JS, like (new Image).src="some.url".

然后你会发现重定向将停止,因为我们除外。也适用于 xhr 和普通 JS,如(new Image).src="some.url".

One more thing, the max redirections limitation is different between browsers. You can test and find the number by redir.php yourself, or find it from this topic: In Chrome, how many redirects are "too many"?

还有一件事,浏览器之间的最大重定向限制是不同的。可以自己通过redir.php测试查找数量,也可以从这个话题中查找:在Chrome中,有多少重定向“太多”了?

Glad if helpful :)

很高兴如果有帮助:)