如何从Angular 2响应中获取所有标题? [英] How to get all the headers from an Angular 2 response?

查看:218
本文介绍了如何从Angular 2响应中获取所有标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在Chrome开发工具中看到的响应标题与Angular 2的响应标题不匹配显示在Chrome控制台中。



< img src =https://i.stack.imgur.com/hgVtC.pngalt =只显示内容类型>



只Content-Type头部在执行后显示:

  this.http.get(tempUrl).map(res => ; {
console.log(csrf received);
console.log(res);
})


解决方案

默认情况下只显示一些安全标题列表(以JavaScript表示)。这是出于安全原因。这个列表如下:


  • 缓存控制

  • 内容语言

  • 内容类型

  • 过期

  • 上次修改

  • Pragma



为了公开其他头文件,服务器应该发送访问控制头 Access-Control-Expose-Headers ,列出所有想要公开的头文件。

  Access-Control-Expose-Headers:Content-Length,X-CSRF-令牌


The response headers I see in Chrome Dev tools don't match the ones that Angular 2 is showing in the Chrome console.

Only the Content-Type header is showing up after having executed:

this.http.get(tempUrl).map(res=>{
      console.log("csrf received");
      console.log(res);
})

解决方案

Only a certain list of "safe" headers are exposed by default (to Javascript). This is for security reasons. This list is as follows

  • Cache-Control
  • Content-Language
  • Content-Type
  • Expires
  • Last-Modified
  • Pragma

In order to expose other headers, the server should send the access control header Access-Control-Expose-Headers, listing all the headers it wants to expose.

Access-Control-Expose-Headers: Content-Length, X-CSRF-Token

这篇关于如何从Angular 2响应中获取所有标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆