【发布时间】:2019-04-23 04:19:51
【问题描述】:
我正在尝试使用以下代码在 http 标头中设置身份验证令牌。它不会在标头中设置身份验证令牌。我在这里想念什么?尝试了以下链接中提供的解决方案,但没有成功。
https://stackoverflow.com/questions/54820257/angular-7-httpinterceptor-not-adding-header-to-request
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const token: string = sessionStorage.getItem('token');
request = request.clone({ url: `${this.baseUrl}/${request.url}` });
const clonedRequest = request.clone({
headers: new HttpHeaders({
Authorization: token,
"Content-Type": "application/json"
})
});
return next.handle(clonedRequest);
}
【问题讨论】:
-
您确定
token不为空吗?你可以试试sample from here -
token 不为空......事实上我也无法在标题中看到内容类型
-
你试过
setHeaders而不是headers吗?该请求也被克隆了两次。有必要吗? -
我尝试使用
setHeaders它设置 request.headers.headers.lazyupdate 中的值