【问题标题】:Angular 2 access header informations from get requestAngular 2 从获取请求中访问标头信息
【发布时间】:2017-02-16 09:46:32
【问题描述】:

restservice 在 get 请求中提供标头信息。如何通过响应访问它们?

我只在回复中得到这些标题信息:

Array[3]
   0:{"content-type" => Array[1]}
   1:{"access-control-allow-origin" => Array[1]}
   2:{"access-control-allow-credentials" => Array[1]}

// Angular request
this._http.get("http://anyUrl/restservice/list")
  .map((response:Response) => {
    console.log(response);//<-- gives the above information but nothing more
    return response.json();
  });

【问题讨论】:

  • 在您的map() 中,尝试阅读response.headers 而不是response.json()
  • 如果您在 response 对象中看不到您要查找的内容,这可能意味着它一开始就不存在(即服务器没有设置您想要的标头)。我认为 Angular 在将响应交给您之前不会对其进行清理。尝试使用Postman 运行相同的请求,看看是否得到不同的标头。
  • 我可以在 chrome 开发工具中看到标头比响应中的标头多。也许 .map 操作会进行任何类型的过滤?
  • 我无法重现它:获取所有标题作为响应。你的最终目标是什么,得到一些特定的标题?
  • 奇怪...在您的示例中,您在map() 之前登录没有任何作用,所以不是这样。你确定console.log(response.headers)时只看到3个成员吗?

标签: angular get


【解决方案1】:

这个问题已经在github上提出了。

查看https://github.com/angular/angular/issues/5237

【讨论】:

    【解决方案2】:

    解决这个问题的方法是在你的 api 的头信息中添加“Access-Control-Expose-Headers”。由于 jigar gala 仅引用了一个链接,因此我将其发布为新的回复以明确这一点。

    header("Access-Control-Expose-Headers: X-Custom-header");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-13
      • 2017-09-22
      • 1970-01-01
      • 1970-01-01
      • 2016-01-28
      • 1970-01-01
      相关资源
      最近更新 更多