使用提取时未发送授权标头 [英] Authorization header not being sent when using fetch

查看:53
本文介绍了使用提取时未发送授权标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试将Authorzation标头设置为以下标头时,该标头不会发送到请求的服务器.用fetch设置Authorization标头的正确方法是什么?

When I try and set the Authorzation header as below the header doesn't get sent to the server for the request. What's the correct way to set the Authorization header with fetch?

let options = { 
  method: 'GET', 
  headers: new Headers({ 
    Authorization: 'Bearer ...' 
  }) 
};
fetch('/api/somedata', options).then(function(response) { console.log(response); };

修改

在网络标签上的chrome开发人员工具中,我收到此请求:

In chrome developer tools on the network tab I get this for the request:

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

请注意,未设置任何授权标头.

Note there's no Authorization header being set.

服务器响应:

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

推荐答案

我相信您的服务器需要包含以下响应标头:

I believe your server needs to include the following response header:

Access-Control-Allow-Headers: Authorization

这篇关于使用提取时未发送授权标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆