从 Angular 中的 HttpClient 发布请求获取响应标头? [英] Getting response headers from HttpClient post request in Angular?

查看:65
本文介绍了从 Angular 中的 HttpClient 发布请求获取响应标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to get the response headers from a post request, but the HttpResponse object doesn't contain the same headers that I can see in the network. What am I doing wrong? I need to access the value of the Apiproxy-Session-Id key and it isn't present in the HttpHeaders.

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

This is my code to execute the post request and log the full response, where http is an HttpClient object.

this.http.post('http://localhost:8081/user/login',JSON.stringify(requestBody), {observe: 'response'}).subscribe(resp => {控制台日志(响应);});

这是我记录的响应.

这些是我在网络中看到的标头.

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

I'm new to Angular and very stumped by this. Thanks for your help!

推荐答案

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

Hey man I was having the same problem here. Since your backend and frontend are on different domains some headers of the response are not exposed by default.

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

You can try two different approaches:

1.在 Angular 上代理您的请求

这样,代理将使您的后端认为请求来自 sabe 域.请参阅此文档了解如何去做吧.请记住,使用此选项所有标题都将公开.

With this the proxy will make your backend think the request came from the sabe domain. See this doc to know how to do it. Keep in mind that with this option all headers will be exposed.

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

因为我不知道你后端的语言,所以我不能告诉你一步一步这样做,但我必须在响应中做这样的事情:response.add("Access-Control-Expose-Headers", "Apiproxy-Session-Id").

Since I don't know the language of your backend I can't tell you the step by step to do so, but I'll have to do something like this on the response: response.add("Access-Control-Expose-Headers", "Apiproxy-Session-Id").

这篇关于从 Angular 中的 HttpClient 发布请求获取响应标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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