【发布时间】: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