【发布时间】:2019-09-12 11:41:58
【问题描述】:
我正在尝试请求一个需要使用令牌进行身份验证的 REST API。在构造 Request 对象时,一些标头会消失。 为什么我不能设置我的授权标头?
let http_headers = {
"Content-type": "application/json",
'Authorization': 'Token token='+my_token,
'Accept': 'Application/json'
};
let url = this.base_url + '/api/v1/test';
let init = {
method: "POST",
headers: new Headers(http_headers),
mode: 'no-cors',
credentials: 'omit' // I try that, but it doesn't seem to have effect
};
let req = new Request( url, init );
console.log(req.headers.get("Accept")); // Application/json
console.log(req.headers.get("Authorization")); // null, why ?
【问题讨论】:
-
什么是
Request?是this module吗?或者this one?您使用的是 Node.js 还是浏览器?当您使用一些Request对象而不是fetch时,为什么会标记为 fetch-api? -
你说
mode: 'no-cors',- 这是一个跨域请求吗?还是同源请求? -
@Quentin 这是来自 fetch API 的请求:developer.mozilla.org/en-US/docs/Web/API/Request。我最初(并打算)使用 fetch(url,init)。该网址来自另一个域。
-
你在哪里定义
my_token?
标签: javascript fetch-api request-headers