HttpClient:无法访问响应头 [英] HttpClient: Can´t access to response headers

查看:144
本文介绍了HttpClient:无法访问响应头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个项目中,我们同时使用了Http& HttpClient获取头部参数。 Http返回标题参数,但HttpClient没有。

In a project, we used both Http & HttpClient to fetch the header params. Http returns the header params, but HttpClient doesn't.

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返回带标题的响应,但httpClient返回空标题中的数组。

When checked in console, http returns the response with headers but httpClient returns an empty array in headers.

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

When checked in networks tab of browser inspector, it displays all the header parameters.

服务器接受以下CORS选项:

The server accepts the following CORS options:

  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获取标题。

Please help me figure out how to fetch the headers using httpClient.

提前致谢。

推荐答案

你正在使用HttpClient走上正轨。您应该在标题上使用 get()方法。

You are on the right track with the HttpClient. You should use the get() method on your headers.

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

其中 my-header 是标题回复你想要获取。

Where my-header is the header response you are trying to fetch.

这篇关于HttpClient:无法访问响应头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆