首页 文章

从Angular中的HttpClient post请求获取响应头?

提问于
浏览
3

我正在尝试从post请求中获取响应头,但是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的新手并且非常难过 . 谢谢你的帮助!

2 回答

  • 0

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

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

    1. Proxy your requests on Angular

    有了这个,代理将使你的后端认为请求来自sabe域 . 请参阅this doc以了解如何操作 . 请记住,使用此选项将显示 all headers .

    2. Expose the headers you want on your backend server

    既然我没有告诉你一步一步这样做,但我必须在回复中做这样的事情: response.add("Access-Control-Expose-Headers", "Apiproxy-Session-Id") .

  • 4

    尝试添加 responseType: 'text'

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

相关问题