在角度6中检索标头有问题吗? [英] Problem with retrieving headers in angular 6?

查看:118
本文介绍了在角度6中检索标头有问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的service

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

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

And in my component I am calling it like this

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

这是我的res.headers

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

具有一个为空的标头对象.在此api的后端中,我发送了一个名为location的标头,该标头显示在Chrome的网络"标签中.一切都通过chrome的网络标签运行,但是从角度来看,我无法检索到此标头.

which has a headers object which is empty. In the backend of this api, I am sending a header called location which is being shown in the network tab of Chrome. Everything is working through chrome's network tab, but in angular, I am unable to retrieve this header.

推荐答案

您是否尝试过阅读完整的回复?

Have you tried reading the full response?

HttpClient读取完整响应

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公开 location 标头.请参阅此链接以供参考: 使用CORS

Also try to expose your location header with Access-Control-Expose-Headers on your backend. See this link for reference: 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标头.此标头的值是您要向客户端公开的响应标头的逗号分隔列表.

这篇关于在角度6中检索标头有问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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