我从一个用 NodeJS 编写的 Express 服务器设置 header :
app.use('/routes', function(req, res, next) {
res.setHeader('test', 'test');
next();
);
它们被发送到客户端,如下图所示: 然后我尝试像这样在 Angular 端检索它们:
return next.handle(newreq).do((event: HttpEvent<any>) => {
if (event instanceof HttpResponse) {
console.log(event);
}
}
});
但是,它不会显示在 console.log(event)
我还尝试使用 event.headers.get('test')
得到相同的结果
谢谢。
最佳答案
作为 CORS 限制的一部分,Angular 客户端只能访问 Access-Control-Expose-Headers
中列出的 header 。只需将新的 test
header 添加到列表中即可。如果您使用 CORS 库,它应该有一种将 header 列入白名单的方法。
请参阅 MDN web docs 中的示例.
关于javascript - 无法获取标题 Angular 6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55907257/