【问题标题】:HttpClient Subscribed Response Headers UndefinedHttpClient 订阅的响应标头未定义
【发布时间】:2018-03-26 17:18:54
【问题描述】:

谁能告诉我为什么我在从 http.post 收到回复时没有收到任何标题?

this.http.post<Response>(url, body).subscribe((res: Response) => console.log(res.headers));

res.headers 记录“未定义”,但在 Chrome DevTools 中,响应显示我正在寻找的标头。

旁注:我确实将 Access-Control-Expose-Headers 设置为我想要公开的标题。

知道为什么会这样吗?

【问题讨论】:

    标签: node.js angular rest observable


    【解决方案1】:

    要观察响应而不是类型转换的主体,较新的 HttpClient 默认会这样做:

    this.http.post(url, data, {observe: 'response'}).subscribe((res:Response) => {});
    

    即。您所做的只是将您的服务器发送的数据类型转换为&lt;Response&gt;

    【讨论】:

      【解决方案2】:
      Just import Response from `@angular/http` or `@angular/common/http`
      reference official doc : https://angular.io/guide/http
                               https://alligator.io/angular/angular-5/
                               https://blog.angular.io/version-5-0-0-of-angular-now-available-37e414935ced
      

      【讨论】:

      • 你知道他的代码不导入就无法构建吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-25
      • 2020-08-22
      • 2019-09-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多