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

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

问题描述

我正在尝试从帖子请求中获取响应标头,但是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.

这是执行post请求并记录完整响应的代码,其中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 => {
console.log(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 post请求获取响应头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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