当我尝试将Authorzation标头设置为如下所示时,标头不会被发送到服务器以获取请求。使用fetch设置Authorization标头的正确方法是什么?
let options = {
method: 'GET',
headers: new Headers({
Authorization: 'Bearer ...'
})
};
fetch('/api/somedata', options).then(function(response) { console.log(response); };
修改
在网络标签上的chrome developer工具中,我得到了以下请求:
GET /api/somedata HTTP/1.1
Host: someserver.azurewebsites.net
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36
Accept: */*
Referer: http://localhost:3000/somedata
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8
请注意,没有设置授权标头。
服务器响应:
HTTP/1.1 401 Unauthorized
Content-Length: 61
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/8.0
WWW-Authenticate: Bearer
X-Powered-By: ASP.NET
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE
Date: Thu, 29 Sep 2016 03:16:15 GMT
答案 0 :(得分:1)
我相信您的服务器需要包含以下响应标头:
Access-Control-Allow-Headers: Authorization