问题描述
有人能给我任何原因,为什么我从http.post返回响应时却没有任何标题?
can anyone give me any reasons on why I'm not getting any headers when getting a response back from http.post?
this.http.post<Response>(url, body).subscribe((res: Response) => console.log(res.headers));
res.headers记录为未定义",但是在Chrome DevTools中,响应显示了我要查找的标题.
res.headers logs 'undefined', however in Chrome DevTools, the response is showing the headers I'm looking for.
侧面说明:我确实将Access-Control-Expose-Headers设置为我要公开的标头.
Side note: I do have Access-Control-Expose-Headers set to the header I'm wanting to expose.
知道为什么会这样吗?
推荐答案
观察响应,而不是类型转换的正文,默认情况下,较新的HttpClient会这样做:
To observe a response instead of a typecasted body, which the newer HttpClient does by default:
this.http.post(url, data, {observe: 'response'}).subscribe((res:Response) => {});
即.您所做的只是键入强制转换服务器以<Response>
发送的数据.
Ie. what you were doing was just type casting the data your server sent as <Response>
.
这篇关于HttpClient订阅的响应标头未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!