首页 > 解决方案 > HttpClient 未返回所有响应标头

问题描述

我同意这是一个重复的问题,但我已经尝试过其他帖子中给出的答案,但这不起作用,所以我用我的代码发布我的

我有一种情况,我正在使用自定义标头进行 GET 调用,并且响应还包含自定义标头。使用 HttpClient 获得的响应仅包含 4 个标头,但不包含从我的服务器发送的自定义标头。我可以在 chome 的开发工具的网络选项卡中看到自定义标题。下面是我正在使用的代码

doGetFullResponse(path: string, headerExtra: any) {
    let headers = new HttpHeaders();
    let authData = localStorage.getItem("user");
    if(authData) {
      headers = headers.append('Authorization', "BEARER "+authData);
      if(headerExtra) {
        for(let key in headerExtra) 
          headers[key] = headerExtra[key];
      }
      //httpClient is HttpClient from '@angular/common/http'
      return this.httpClient.get(path, {headers: headers, observe: "response"});
    }
    else {
      this.router.navigateByUrl("/");
    }
  }

我称这个函数为

this.httpservice.doGetFullResponse().subscribe((res)=>{
      //this.pointAudits = res.json();
      res.headers.keys().map((key)=>console.log(key+":"+res.headers.get(key)));
    })

下面是输出:

pragma:no-cache content-type:application/json;charset=UTF-8 cache-control:no-cache, no-store, max-age=0, must-revalidate expires:0

从我的服务器“ MAX_ITEM_COUNT”发送的自定义标头不会出现在此处。有什么我想念的吗?我正在使用 Angular 4。如果需要任何细节,请告诉我。

标签: angularangular4-httpclient

解决方案


推荐阅读