简体   繁体   English

无法获得头部Angular 6

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM