Javascript 获取 api : Can't retrieve some response header

标签 javascript cors fetch-api

我正在使用 javascript fetch API 来查询跨域 api,使用此代码:

fetch('https://api.com/search?query="2016"').then(function (response) {

  console.log(response.headers.get('Access-Control-Allow-Headers'))

  console.log(response.headers.get('Content-Range'))
  console.log(response.headers.get('Accept-Range'))

  console.log(response.headers.get('Date'))
  console.log(response.headers.get('Content-Type'))
})

响应头如下:

Accept-Range:cars 300
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:Origin, X-Requested-With, Content-Type, Accept, Content-Range, Accept-Range
Access-Control-Allow-Methods:PUT, POST, OPTIONS, GET
Access-Control-Allow-Origin:*
Cache-Control:no-cache
Connection:keep-alive
Content-Range:0-99/1941
Content-Type:application/json
Date:Tue, 12 Jan 2016 12:17:55 GMT
Transfer-Encoding:chunked

非常奇怪的是,只有“Content-Type”对我得到的其他人有效 null :

null
null
null
null
application/json

我需要做什么来检索那些 null header ?

最佳答案

该死的,我使用的是“Access-Control-Allow-Headers”而不是“Access-Control-Expose-Headers”

现在可以了

关于Javascript 获取 api : Can't retrieve some response header,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34743617/

相关文章:

javascript - 具有身份验证的 Cloud Firestore

html - 在 React Firebase Web 应用程序中使用 Google Adsense 时具有 CORS

java - 如何关闭与netty CorsHandler的连接

javascript - 如何在 JavaScript 中将 Ajax 转换为 Fetch API?

javascript - 如何使用 then() 将 Fetch 响应的 JSON 主体传递给 Throw Error()?

javascript - 无法使用 Fetch API 从本地主机加载 Deezer API 资源

javascript - 如何判断域名是http还是https?

javascript - 用于扩展/继承 HTML 模板的 JS 库

javascript - 如何在 Windows Phone 7 上调试 Internet Explorer?

angular - Angular 2 应用程序中没有 'Access-Control-Allow-Origin' header