在角度6中检索标题有问题吗?

问题描述 投票:0回答:1

这是我的service

executeProcess(): Observable<any> {
    return this._httpClient.post(this.baseUrl + '/api/survey/process/execute', {}, { observe: 'response', responseType: 'text' as 'text' });
}

在我的组件中,我这样称呼它

this._httpService.executeProcess().subscribe(res => {
      console.log(res);
}

这是我的res.headers

{
  "normalizedNames": {},
  "lazyUpdate": null,
  "headers": {}
}

其头部对象为空。在这个api的后端,我发送了一个名为location的标题,它正在Chrome的网络标签中显示。一切都在通过chrome的网络选项卡工作,但在角度,我无法检索此标题。

angular header http-headers observable
1个回答
0
投票

你试过阅读完整的回复吗?

HttpClient reading the full response

showConfigResponse() {
  this.configService.getConfigResponse()
    // resp is of type `HttpResponse<Config>`
    .subscribe(resp => {
      // display its headers
      const keys = resp.headers.keys();
      this.headers = keys.map(key =>
        `${key}: ${resp.headers.get(key)}`);

      // access the body directly, which is typed as `Config`.
      this.config = { ... resp.body };
    });
}

编辑:

同时尝试在后端使用Access-Control-Expose-Headers公开您的位置标题。请参阅此链接以供参考:Using CORS

Access-Control-Expose-Headers(可选) - XMLHttpRequest 2对象具有getResponseHeader()方法,该方法返回特定响应头的值。在CORS请求期间,getResponseHeader()方法只能访问简单的响应头。简单响应头定义如下:

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

如果希望客户端能够访问其他标头,则必须使用Access-Control-Expose-Headers标头。此标头的值是您要向客户端公开的响应标头的逗号分隔列表。

© www.soinside.com 2019 - 2024. All rights reserved.