Javascript fetch api:无法检索一些响应标头

时间:2016-01-12 12:23:34

标签: 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 标题?

2 个答案:

答案 0 :(得分:7)

该死的我使用'Access-Control-Allow-Headers'代替'Access-Control-Expose-Headers'

现在可以使用

答案 1 :(得分:0)

使用CORS软件包。并输入以下参数:

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

like在这里写道:https://stackoverflow.com/a/64536201/2591785