Cant get headers of response in angular 5

前端 未结 3 1392
深忆病人
深忆病人 2021-01-14 13:44

I make a post request to a server which responds with two headers that are important for the client: username and access-token. The Network Tab of the Chrome debug tool disp

相关标签:
3条回答
  • 2021-01-14 13:59

    Just want to give additional info. Yesterday spent on this few hours and was getting crazy.

    Once you have access-control-expose-headers from backend. And if you just printing response object like this console.log(res) or console.log(res.headers) it will not show the headers because Angular uses Lazy-loading. You need to explicitly say which header you need console.log(res.headers.get('your-header'))

    0 讨论(0)
  • 2021-01-14 14:04

    normally the response is just the 'body', while you want the 'headers'. on the res you could use:

    this.header = res.headers.get('header-name');
    
    0 讨论(0)
  • 2021-01-14 14:13

    The front end will not be able to access that header unless the back end allows it.

    In order to do so, you need to send a Access-Control-Expose-Headers header from the backend, and the value should be a comma separated list of the values you want to expose, i.e. access-token, username

    0 讨论(0)
提交回复
热议问题