Javascript 如何在 XMLHttpRequest 中获取响应 url?

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

How to get response url in XMLHttpRequest?

javascriptajaxxmlhttprequestresponse

提问by Okan Kocyigit

There is a page (url), I request it by XMLHttpRequest, but I'm not getting response from requested url, It's directing request to another page,

有一个页面(url),我通过 XMLHttpRequest 请求它,但是我没有从请求的 url 得到响应,它正在将请求定向到另一个页面,

requesting --- > page.php
getting reponse from > directedpage.php

请求 --- > page.php
从 > directedpage.php得到响应

and the question is how can I get response url? (directedpage.php in example)

问题是如何获得响应网址?(示例中的directedpage.php)

回答by Rob W

The final URL (after following all redirects) is available in the responseURLattributeof a XMLHttpRequestinstance. This feature is new and only supported in Firefox 32 stable in September 2014 [ref]and Chrome 37.0.2031.0 stable in August 2014 [ref](and probably also Opera 24). responseURLis not (yet) supported in IE 11 or Safari 7 and older browsers. For these browsers, the previous answer is still true:

最终 URL(遵循所有重定向之后)在实例的responseURL属性中可用XMLHttpRequest。此功能是新功能,仅在2014 年 9 月的Firefox 32稳定版[ref]2014 年 8 月的Chrome 37.0.2031.0稳定版[ref](可能还有 Opera 24)中受支持。responseURLIE 11 或 Safari 7 及更旧的浏览器(尚)不支持。对于这些浏览器,之前的答案仍然成立:

XMLHttpRequestautomatically follows redirects, without saving the served URLs in a property. The Locationheader can neither be retrieved through the .getResponseHeader().

XMLHttpRequest自动跟随重定向,而不将提供的 URL 保存在属性中。的Location报头可以既不通过检索.getResponseHeader()

References:

参考: