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

查看:44
本文介绍了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天全站免登陆