我使用 http 拦截器为每个请求添加一个身份验证 token ,但对于一个请求,我必须添加另一个 token 来代替身份验证 token 。我的问题是我无法设置请求的 header 。
我尝试过这些,但它们都不能正常工作。这些在有效负载中设置 header ,而不是在实际 header 中。
header = new HttpHeaders({'Content-Type': 'application/json'});
header2: HttpHeaders = new HttpHeaders();
constructor(private httpClient: HttpClient) {
this.header2.append('Authorization', this.getRefreshToken());
}
ngOnInit() {
this.httpClient.post('http://localhost:3001/api/token-exchange', { headers: this.header2}).subscribe()
}
即使它能工作,拦截器也会覆盖它,所以我尝试使用HttpBackend,但那也不起作用。
constructor(
private httpClient: HttpClient,
handler: HttpBackend
) {
this.httpClient = new HttpClient(handler);
this.header2 = this.header2.set('Authorization', this.getRefreshToken());
}
最佳答案
阻止这种情况的一种方法是检查拦截器中 http 请求的 URL,并将该 URL 从您要应用的正常逻辑中排除。然后像平常一样在 http 调用中添加所需的 header 。
关于javascript - Angular无法设置http header ,只能使用拦截器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58222030/