javascript - 无法获取标题 Angular 6

标签 javascript node.js angular express

我从一个用 NodeJS 编写的 Express 服务器设置 header :

app.use('/routes', function(req, res, next) {
    res.setHeader('test', 'test');
    next();
);

它们被发送到客户端,如下图所示: Headers 然后我尝试像这样在 Angular 端检索它们:

return next.handle(newreq).do((event: HttpEvent<any>) => {
    if (event instanceof HttpResponse) {
        console.log(event);
    }
}
});

但是,它不会显示在 console.log(event)

的输出中

Headers output 我还尝试使用 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/

相关文章:

javascript - 为什么 Sails JS 无法识别我的自定义策略

javascript - 在我的 Windows 机器上安装 Grunt 时出现问题

node.js - 当相同的node.js包被不同模块用作依赖项时,如何避免多次安装?

javascript - 单击按钮以 Angular 4 滚动 div

html - 如何在 Angular 4 中为表格添加分页?

javascript - 收到错误 .append 不是函数

Javascript forEach - 如何循环一个对象?

javascript - FullCalendar 在周时间轴 View 中隐藏时间

javascript - 如何抑制c# WebBrowser控件中的 'stop running this script'提示?

javascript - typescript 错误 : Type 'undefined[]' is not assignable to type 'void'