Angular 7 个 HttpHeader 为空
Angular 7 HttpHeaders are empty
我知道 HttpHeader 是不可变的。我尝试了几种不同的方法来初始化它们或附加到它们,但是当我记录它们时,它总是显示它们是空的。
我已经尝试用所有值初始化它们:
const headers = new HttpHeaders({
'Authorization': `Bearer ${this.auth.accessToken}`,
'Content-Type': 'application/json'
});
我试过设置:
let headers = new HttpHeaders();
headers = headers.set('Authorization', `Bearer ${this.auth.accessToken}`);
headers = headers.set('Content-Type', 'application/json');
我试过追加:
let headers = new HttpHeaders();
headers = headers.append('Authorization', `Bearer ${this.auth.accessToken}`);
headers = headers.append('Content-Type', 'application/json');
但是所有结果都是空的 headers 并且 API 表示无法找到任何授权令牌。
控制台输出:
我做错了什么?
这可能是由于 lazy parsing,您必须执行 get
才能访问值。未作为键值对实现。
HttpHeaders class represents the header configuration options for an
HTTP request. Instances should be assumed immutable with lazy parsing.
做console.log(headers.getAll('Authorization'))
但是,为了确保 headers 已发送,更好的检查位置是 DevTools 中的 “网络”选项卡。
我知道 HttpHeader 是不可变的。我尝试了几种不同的方法来初始化它们或附加到它们,但是当我记录它们时,它总是显示它们是空的。
我已经尝试用所有值初始化它们:
const headers = new HttpHeaders({
'Authorization': `Bearer ${this.auth.accessToken}`,
'Content-Type': 'application/json'
});
我试过设置:
let headers = new HttpHeaders();
headers = headers.set('Authorization', `Bearer ${this.auth.accessToken}`);
headers = headers.set('Content-Type', 'application/json');
我试过追加:
let headers = new HttpHeaders();
headers = headers.append('Authorization', `Bearer ${this.auth.accessToken}`);
headers = headers.append('Content-Type', 'application/json');
但是所有结果都是空的 headers 并且 API 表示无法找到任何授权令牌。
控制台输出:
我做错了什么?
这可能是由于 lazy parsing,您必须执行 get
才能访问值。未作为键值对实现。
HttpHeaders class represents the header configuration options for an HTTP request. Instances should be assumed immutable with lazy parsing.
做console.log(headers.getAll('Authorization'))
但是,为了确保 headers 已发送,更好的检查位置是 DevTools 中的 “网络”选项卡。