Angular 8 post 请求获取 response header 信息

2,418 阅读1分钟

在post请求中添加 observe 选项来告诉 HttpClient,你想要完整的响应对象

getConfigResponse(): Observable<HttpResponse<Config>> {
  return this.http.get<Config>(
    this.configUrl, { observe: 'response' });
}

然后在 getConfigResponse() 的订阅中可以获取headers

getConfigResponse().subscribe( res => {
    res.headers.get('Content-Type');  // 'application/json'
})

有可能在返回的headers中没有想要的值,那可能是 服务端 没有设置 access-control-espose-headers

参考
官方文档,读取完整的响应体 angular.cn/guide/http#…
StackOverflow stackoverflow.com/questions/4…