[英]Can't get header Angular 6
I set the headers from an Express server written in NodeJS like that: 我从NodeJS编写的Express服务器中设置标题,如下所示:
app.use('/routes', function(req, res, next) {
res.setHeader('test', 'test');
next();
);
And they are sent to the client as it can be seen in the following image: 它们被发送到客户端,如下图所示:
Then I try to retrieve them in the Angular side like that:
然后我尝试在Angular端检索它们,如下所示:
return next.handle(newreq).do((event: HttpEvent<any>) => {
if (event instanceof HttpResponse) {
console.log(event);
}
}
});
But then, it's not displayed in the output of console.log(event)
但是,它没有显示在
console.log(event)
的输出中console.log(event)
I also tried to use
event.headers.get('test')
with the same result 我也尝试使用相同结果的
event.headers.get('test')
thanks. 谢谢。
Only headers listed in Access-Control-Expose-Headers
can be accessed by the Angular client, as part of CORS restrictions. 作为CORS限制的一部分,Angular客户端只能访问
Access-Control-Expose-Headers
列出的标头。 Simply add your new test
header to the list. 只需将新
test
标题添加到列表中即可。 If you use a CORS library it should have a method to whitelist headers. 如果您使用CORS库,它应该有一个白名单标题的方法。
See examples from MDN web docs . 请参阅MDN Web文档中的示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.