問題描述
是否可以發送 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.
這篇關于XHR HEAD 請求是否有可能不遵循重定向 (301 302)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!