【问题标题】:Getting response headers from HttpClient post request in Angular?从 Angular 中的 HttpClient 发布请求获取响应标头?
【发布时间】:2018-01-26 03:44:15
【问题描述】:

我正在尝试从发布请求中获取响应标头,但 HttpResponse 对象不包含我在网络中可以看到的相同标头。我究竟做错了什么?我需要访问 Apiproxy-Session-Id 键的值,它不存在于 HttpHeaders 中。

这是我执行 post 请求并记录完整响应的代码,其中 http 是一个 HttpClient 对象。

this.http.post('http://localhost:8081/user/login', JSON.stringify(requestBody), {observe: 'response'}).subscribe(resp => { console.log(resp); });

This is the response I'm logging.

These are the headers I'm seeing in the network.

我是 Angular 的新手,对此感到非常困惑。谢谢你的帮助!

【问题讨论】:

  • 您的问题解决了吗?

标签: angular http-headers httpresponse angular-http angular-httpclient


【解决方案1】:

嘿,伙计,我在这里遇到了同样的问题。由于您的后端和前端位于不同的域中,因此默认情况下不会公开响应的某些标头。

您可以尝试两种不同的方法:

1.在 Angular 上代理您的请求

这样,代理将使您的后端认为请求来自 sabe 域。请参阅this doc 了解如何操作。请记住,使用此选项所有标题都将被公开。

2。在后端服务器上公开您想要的标头

由于我不知道您的后端语言,我无法告诉您一步一步这样做,但我必须在响应中做这样的事情:@ 987654322@.

【讨论】:

  • 谢谢!真的很有帮助。这应该被接受的答案
【解决方案2】:

尝试添加responseType: 'text'

this.http.post('http://localhost:8081/user/login', 
    JSON.stringify(requestBody), {observe: 'response', responseType: 'text'}).subscribe(resp => {
        console.log(resp);
    });

【讨论】:

    猜你喜欢
    • 2018-07-31
    • 2019-03-04
    • 2017-09-15
    • 2020-05-14
    • 2019-06-20
    • 1970-01-01
    • 2018-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多