响应标头不适用于带有"redirect:manual"的提取请求 [英] Response headers not available for fetch request with 'redirect: manual'

查看:174
本文介绍了响应标头不适用于带有"redirect:manual"的提取请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做

        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开发工具中的响应标头:

and the reponse headers from chrome dev tools:

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

是否无法在重定向响应中获取响应头?

Is it not possible to get response headers out on redirect response?

推荐答案

是否无法在重定向响应中获取响应头?

Is it not possible to get response headers out on redirect response?

否,这是不可能的. Fetch规范中的要求阻止了这种情况.

No, it’s not possible. The requirements in the Fetch spec prevent it.

对于redirect: "manual",问题显示的内容是预期的.具体来说,响应redirect: "manual"请求,暴露给前端JS的标头对象应该为空.

What the question shows is expected for redirect: "manual". Specifically, the headers object exposed to frontend JS is expected to be empty in responses to redirect: "manual" requests.

更多详细信息:当请求设置redirect: "manual"时,响应类型为opaqueredirect.有关效果的信息,请访问 https://developer. mozilla.org/zh-CN/docs/Web/API/Response/type :

More details: When a request sets redirect: "manual", the response type is opaqueredirect. Info on the effects of that is at https://developer.mozilla.org/en-US/docs/Web/API/Response/type:

opaqueredirect:使用redirect: "manual"发出了获取请求.响应的状态为0,标题为空,正文为空,尾部为空.

opaqueredirect: The fetch request was made with redirect: "manual". The Response's status is 0, headers are empty, body is null and trailer is empty.


该MDN文章中的这些详细信息直接基于Fetch规范的以下部分:


Those details in that MDN article are based directly on the following parts of the Fetch spec:

https://fetch.spec.whatwg.org/#concept -request-redirect-mode

请求具有关联的重定向模式,该模式为"follow""error""manual".

手动" :当请求遇到重定向请求时,检索不透明重定向过滤的响应,以便可以手动进行重定向.

A request has an associated redirect mode, which is "follow", "error", or "manual".

"manual": Retrieves an opaque-redirect filtered response when a request is met with a redirect so that the redirect can be followed manually.

https://fetch.spec.whatwg.org/#concept-filtered-response-opaque-redirect

不透明重定向过滤的响应是过滤的响应,其类型为"opaqueredirect",状态为0,状态消息为空字节序列,标题列表为空,正文为空

不透明的过滤响应和不透明的重定向过滤响应与网络错误几乎没有区别

opaque-redirect filtered response is a filtered response whose type is "opaqueredirect", status is 0, status message is the empty byte sequence, header list is empty, body is null

an opaque filtered response and an opaque-redirect filtered response are nearly indistinguishable from a network error

这篇关于响应标头不适用于带有"redirect:manual"的提取请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆