HttpClient Subscribed Response Headers Undefined

只愿长相守 提交于 2019-12-31 03:35:06

问题


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 logs 'undefined', however in Chrome DevTools, the response is showing the headers I'm looking for.

Side note: I do have Access-Control-Expose-Headers set to the header I'm wanting to expose.

Any idea why this would be?


回答1:


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) => {});

Ie. what you were doing was just type casting the data your server sent as <Response>.




回答2:


Just import Response from `@angular/http` or `@angular/common/http`
reference official doc : https://angular.io/guide/http
                         https://alligator.io/angular/angular-5/
                         https://blog.angular.io/version-5-0-0-of-angular-now-available-37e414935ced


来源:https://stackoverflow.com/questions/49497114/httpclient-subscribed-response-headers-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!