无法在角度5中获得响应标题 [英] Cant get headers of response in angular 5
问题描述
我向服务器发出发布请求,该服务器以对客户端重要的两个标头进行响应:用户名和访问令牌. Chrome调试工具的网络"标签显示以下数据作为响应:
I make a post request to a server which responds with two headers that are important for the client: username and access-token. The Network Tab of the Chrome debug tool displays the following data for the response:
我还将响应记录在控制台上:
I also log the response on the console:
此处不包含标题-为什么会这样? 我的记录代码:
Here the headers are not present - why is this? My code for logging:
this.usersService.registerNewUser(firstName, lastName, email, username, birthday, password).subscribe(
res => {
console.log(res);
},
err => {
console.log("Error" + JSON.stringify(err));
}
);
我想通过res.headers.username
访问标头用户名.
I wanted to access the header username by res.headers.username
.
我的请求如下:
this.http.post<string>(
"http://localhost:2002/users",
JSON.stringify({
firstName: firstName,
lastName: lastName,
email: email,
username: username,
birthday: birthday,
password: password
}),
{
observe: "response"
}
);
我做错了什么?
推荐答案
除非后端允许,否则前端将无法访问该标头.
The front end will not be able to access that header unless the back end allows it.
为此,您需要从后端发送Access-Control-Expose-Headers
标头,并且该值应该是要公开的值的逗号分隔列表,即access-token, username
In order to do so, you need to send a Access-Control-Expose-Headers
header from the backend, and the value should be a comma separated list of the values you want to expose, i.e. access-token, username
这篇关于无法在角度5中获得响应标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!