我在做
console.log("navigating");
var rsp = await fetch(params.url, {
credentials: "include", redirect: "manual", mode: "cors"
});
console.log(rsp);
rsp.headers.forEach(console.log);
console.log(rsp.headers.get('Location'));
console.log(rsp.headers.get('location'));
和chrome开发工具的响应标头:
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: http://localhost:4400
Access-Control-Expose-Headers: Location
Cache-Control: no-cache
Connection: keep-alive
Content-Length: 0
Date: Fri, 05 Oct 2018 12:48:21 GMT
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Location: http://localhost/test
给予
Response
body: (...)
bodyUsed: falseheaders:
Headers {}
ok: false
redirected: false
status: 0
statusText: ""
type: "opaqueredirect"
url: "..."
index.ts:161 null
index.ts:162 null
是否无法在重定向响应中获取响应头?
答案 0 :(得分:2)
是否无法在重定向响应中获取响应头?
否,这是不可能的。 Fetch规范中的要求阻止了它。
问题显示的数据应用于redirect: "manual"
。具体来说,响应redirect: "manual"
请求,公开给前端JS的标头对象应该为空。
更多详细信息:当请求设置redirect: "manual"
时,响应类型为opaqueredirect
。有关效果的信息位于https://developer.mozilla.org/en-US/docs/Web/API/Response/type:
opaqueredirect
:使用redirect: "manual"
发出了获取请求。响应的状态为0,标题为空,正文为空,尾部为空。
该MDN文章中的这些详细信息直接基于Fetch规范的以下部分:
https://fetch.spec.whatwg.org/#concept-request-redirect-mode
请求具有关联的重定向模式,该模式为
"follow"
,"error"
或"manual"
。
…
“手动” :当请求遇到重定向请求时,检索不透明重定向过滤的响应,以便可以手动进行重定向。
https://fetch.spec.whatwg.org/#concept-filtered-response-opaque-redirect
不透明重定向过滤的响应是过滤的响应,其类型为
"opaqueredirect"
,状态为0
,状态消息为空字节序列,标题列表为空,正文为空
…
不透明过滤响应和不透明重定向过滤响应与网络错误几乎没有区别