How to get headers of the response from fetch(如何从 fetch 中获取响应的标头)
问题描述
我在 chrome 版本 52.0.2743.82(64 位)上使用 fetch.我想获取响应中的所有标题.以下代码段仅返回 content-type
但如果您查看 chrome 开发工具,它会显示许多其他响应标头.如何从 fetch 中获取其他标头.
I am using fetch on chrome Version 52.0.2743.82 (64-bit). 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. How to get other headers from 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();
}
})
我尝试对 github 实现进行 polyfill(手动覆盖).还是没有运气.
I tried polyfilling(manually overriding) the github implementation. Still no luck.
推荐答案
通过ajax请求跨域内容时,无法访问所有header.如果来源相同,您可以访问所有标题.
You can't access all the headers when requesting cross-domain content via ajax. You can access all headers if origin is same.
如 W3 规范此处中所述,只有 Content-Type
、Last-modified
、Content-Language
、Cache-Control
、Expires
、Pragma
标题是可访问的.
As explained in W3 Specifications here, only Content-Type
, Last-modified
, Content-Language
, Cache-Control
, Expires
, Pragma
headers are accessible.
进一步的 https://httpbin.org/get
仅从可访问的标头列表中发送 Content-Type
标头,所以,你只得到了.
Further https://httpbin.org/get
send only Content-Type
header from list of accessible headers so, you got that only.
您可以通过发送 Access-Control-Expose-Headers
带有您希望客户端在响应中访问的标头.
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.
这篇关于如何从 fetch 中获取响应的标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何从 fetch 中获取响应的标头


- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- Fetch API 如何获取响应体? 2022-01-01