【问题标题】:Javascript fetch api : Can't retrieve some response headerJavascript fetch api:无法检索某些响应标头
【发布时间】:2016-04-17 01:34:15
【问题描述】:

我正在使用 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 标头?

【问题讨论】:

  • 该服务是否允许您访问这些内容?
  • OPTIONS 响应是否包括那些?
  • 是的,但它是一个 GET 请求,所以我认为它无论如何都没有使用 OPTIONS
  • 浏览器会先做一个OPTIONS请求。
  • 我在 Chrome 开发者工具中没有看到任何 OPTIONS 请求。但即使我的 nginx 服务器有“add_header 'Access-Control-Allow-Headers' 'Date, Origin, X-Requested-With, Content-Type, Accept, Content-Range, Accept-Range';”

标签: javascript cors fetch-api


【解决方案1】:

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

现在可以了

【讨论】:

  • 这为我节省了很多时间。谢谢!
【解决方案2】:

使用CORS 包。并把这个参数:

cors({credentials: true, origin: true, exposedHeaders: '*'})

喜欢在这里写的:https://stackoverflow.com/a/64536201/2591785

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-06
    • 2020-11-15
    • 1970-01-01
    • 2021-01-22
    • 1970-01-01
    • 2021-11-26
    • 1970-01-01
    • 2018-07-02
    相关资源
    最近更新 更多