我正在使用一个登录终端点,该终端点将承载令牌作为响应标头返回,正如我可以在Chrome检查窗口的“网络”选项卡中看到的那样:
Response Headers
Access-Control-Allow-Credentials:true
Access-Control-Allow-Origin:http://localhost:8100
Authorization:Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJuZWxpby5jdXJzb3NAZ21haWwuY29tIiwiZXhwIjoxNTEyNzA3OTQ3fQ.pOR4WrqkaFXdwbeod1tNlDniFZXTeMXzKz9uU68rLXEWDAVRgWIphvx5F_VCsXDwimD8Q04JrxelkNgZMzBgXA
Cache-Control:no-cache, no-store, max-age=0, must-revalidate
Content-Length:188
(etc...)
然而,当我尝试使用HttpClient实例从响应中打印“headers”时:
authenticate(credentials) {
let creds = JSON.stringify(credentials);
let contentHeader = new HttpHeaders({"Content-Type": "application/json"});
this.http.post(this.LOGIN_URL, creds, { headers: contentHeader, observe: 'response'})
.subscribe(
(resp) => {
console.log("resp-ok");
console.log(resp.headers);
},
(resp) => {
console.log("resp-error");
console.log(resp);
}
);
}
我得到了完全不同的结构:
HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
lazyInit : ƒ ()
lazyUpdate : null
normalizedNames : Map(0) {}
我还尝试了使用get(headerName)方法,但是返回了null。我错过了什么?我该如何从响应中获取“Authorization”头信息?