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

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

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:

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-method 似乎指定应遵循请求,有没有办法阻止它?

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

推荐答案

没有,这不是你可以阻止的暴露行为.

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

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

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

这种方式可以尝试让事情变得更容易,如果资源移动等...但是在设计和制定规范时,所有这些重定向服务都不存在.只是没有强烈需要任何其他行为或能力来阻止它,我认为随着网络上的重定向越来越多,我们不会看到添加的能力,但谁知道每个浏览器何时会支持它.

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.

相关文章