从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")
.