angular - HttpClient : Can´t access to response headers

标签 angular http-headers angular-httpclient

在一个项目中,我们同时使用了 Http 和 HttpClient 来获取 header 参数。 Http 返回 header 参数,但 HttpClient 没有。

constructor(private http: Http, private httpClient: HttpClient) {}
getContent() {
        const url = '';
        return this.http.post(url, data)
            .map((res: HttpResponse<any>) => {
                console.log('http content', res);
            });


        return this.httpClient.post(url, data, { observe: 'response' })
            .map((res: HttpResponse<any>) => {
                console.log('httpClient content',res);
            });
}

在控制台中检查时,http 返回带有 header 的响应,但 httpClient 在 header 中返回一个空数组。

在浏览器检查器的网络选项卡中选中时,它会显示所有 header 参数。

服务器接受以下 CORS 选项:

  origin: '*',
  methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
  allowedHeaders: 'Origin,X-Requested-With,x-access-token,Content-Type,Authorization,Accept,jwt',
  exposedHeaders: 'Content-Type,Authorization,jwt'

请帮我弄清楚如何使用 httpClient 获取 header 。

提前致谢。

最佳答案

您在 HttpClient 的正确轨道上。您应该在 header 上使用 get() 方法。

this.httpClient.post(url, data, {observe: 'response'})
    .map((res: HttpResponse<any>) => {
        let myHeader = res.headers.get('my-header');
        return res;
    });

my-header 是您要获取的 header 响应。

关于angular - HttpClient : Can´t access to response headers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47389365/

相关文章:

html - Angular 2 - 单击div后更改视频src

angular - 如何在 Angular 2.0+ 中获取当前 URL

javascript - @Input 或服务在组件中不起作用

C# HTTP 在获取正文之前发布和只读响应的 header

angular - 在http observable开始网络调用之前执行代码

javascript - ngx-monaco-editor : unable to find loader. 来自 node_modules 的 js 文件

java - Apache HttpClient - Retry-After header

iis - 重写 IIS 7 中响应头的规则(替换 cookie 路径)

angular - 可以在 HttpInterceptor 中获取上传进度事件,但不能从 HttpClient 调用中获取吗?

angular - 如何在不使用 HttpIntercepter 的情况下在 Angular 中获取自定义响应 header