在一个项目中,我们同时使用了 Http 和 HttpClient 来获取 header 参数。 Http 返回 header 参数,但 HttpClient 没有。
constructor(private http: Http, private httpClient: HttpClient) {}
getContent() {
const url = '';
return this.http.post(url, data)
.map((res: HttpResponse<any>) => {
console.log('http content', res);
});
return this.httpClient.post(url, data, { observe: 'response' })
.map((res: HttpResponse<any>) => {
console.log('httpClient content',res);
});
}
在控制台中检查时,http 返回带有 header 的响应,但 httpClient 在 header 中返回一个空数组。
在浏览器检查器的网络选项卡中选中时,它会显示所有 header 参数。
服务器接受以下 CORS 选项:
origin: '*',
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
allowedHeaders: 'Origin,X-Requested-With,x-access-token,Content-Type,Authorization,Accept,jwt',
exposedHeaders: 'Content-Type,Authorization,jwt'
请帮我弄清楚如何使用 httpClient 获取 header 。
提前致谢。
最佳答案
您在 HttpClient 的正确轨道上。您应该在 header 上使用 get()
方法。
this.httpClient.post(url, data, {observe: 'response'})
.map((res: HttpResponse<any>) => {
let myHeader = res.headers.get('my-header');
return res;
});
my-header
是您要获取的 header 响应。
关于angular - HttpClient : Can´t access to response headers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47389365/