且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Angular 5中使用HttpClient访问响应头?

更新时间:2023-02-15 10:04:03

要访问完整的响应(不仅仅是响应的主体),您必须传递 observe:'响应您的http请求中的'参数选项。现在,您可以使用 res.headers访问标题

To access the full response (not just the body of the response), you must pass the observe: 'response' parameter option in your http request. Now you can access the headers with res.headers

return this.http.post('http://127.0.0.1:8080/api/v1/login', {
        'username': username,
        'password': password
    }, {
        headers: new HttpHeaders()
            .set('Content-Type', 'application/json'),
        observe: 'response'
    })
    .map(res => {
        let myHeader = res.headers.get('my-header');
    });

文档