Getting response headers from HttpClient post request in Angular?

不想你离开。 提交于 2019-12-01 20:16:45

Hey man I was having the same problem here. Since your backend and frontend are on different domains some headers of the response are not exposed by default.

You can try two different approaches:

1. Proxy your requests on Angular

With this the proxy will make your backend think the request came from the sabe domain. See this doc to know how to do it. Keep in mind that with this option all headers will be exposed.

2. Expose the headers you want on your backend server

Since I don't know the language of your backend I can't tell you the step by step to do so, but I'll have to do something like this on the response: response.add("Access-Control-Expose-Headers", "Apiproxy-Session-Id").

Try to add responseType: 'text'

this.http.post('http://localhost:8081/user/login', 
    JSON.stringify(requestBody), {observe: 'response', responseType: 'text'}).subscribe(resp => {
        console.log(resp);
    });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!