简体   繁体   English

如何从 fetch 中获取响应的标头

[英]How to get headers of the response from fetch

I am using fetch on chrome Version 52.0.2743.82 (64-bit).我在 chrome 版本 52.0.2743.82(64 位)上使用 fetch。 I want to get all the headers in the response.我想获取响应中的所有标头。 Following snippet only return content-type but if you peek into chrome dev tools it shows many other response headers.以下代码段仅返回content-type但如果您查看 chrome 开发工具,它会显示许多其他响应标头。 How to get other headers from fetch.如何从 fetch 中获取其他标头。

  fetch('https://httpbin.org/get')
    .then(response => {
       const headers = response.headers.entries();
       let header = headers.next();
       while (!header.done){
         console.log(headers.value);
         header = header.next();
       }
    })

I tried polyfilling(manually overriding) the github implementation.我尝试了 polyfilling(手动覆盖)github 实现。 Still no luck.仍然没有运气。

You can't access all the headers when requesting cross-domain content via ajax.通过 ajax 请求跨域内容时,您无法访问所有标头。 You can access all headers if origin is same.如果来源相同,您可以访问所有标题。

As explained in W3 Specifications here , only Content-Type , Last-modified , Content-Language , Cache-Control , Expires , Pragma headers are accessible.正如此处的W3 规范中所述,只有Content-TypeLast-modifiedContent-LanguageCache-ControlExpiresPragma标头可访问。

Further https://httpbin.org/get send only Content-Type header from list of accessible headers so, you got that only.此外, https://httpbin.org/get //httpbin.org/get 仅发送可访问标头列表中的Content-Type标头,因此,您只能得到那个。

Edit: You can expose non-standard CORS response headers by sending Access-Control-Expose-Headers with the headers you want the client to access on the response.编辑:您可以通过发送带有您希望客户端在响应中Access-Control-Expose-Headers标头的Access-Control-Expose-Headers来公开非标准 CORS 响应标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM