【问题标题】:HttpClient.get('/', {observe: 'response'}); not returning all headersHttpClient.get('/', {观察:'response'});不返回所有标题
【发布时间】:2019-08-27 22:46:49
【问题描述】:

我正在使用HttpClient.get() 编写一个 API GET 请求。我传入了观察响应的选项,当我访问.keys()时,除了Content-Type之外,我没有得到任何其他标题。

Access-Control-Allow-Origin 标头设置为通配符 (*),但我仍然只能以角度访问 Content-Type 标头。我错过了什么?

标头已正确发送到网络选项卡中的浏览器。我还尝试在Access-Control-Allow-Origin 中明确允许该自定义标头。

Angular 应用:

this.data.getClubs(1, 10, '', {clubName: 'asc'}, '').subscribe(
      dataJson => {
        console.log(dataJson.headers.keys());
}
getClubs(page: number, count: number, group: any, sorting: any, filter: any) {
    return this.http.get(this.apiUrl + `/club`, {
      params: {
        count: String(count),
        filter: String(filter),
        group: String(group),
        page: String(page),
        sorting: JSON.stringify(sorting)
      },
      headers: this.getHttpOptions().headers,
      observe: 'response'
    });
}

请查看图片。标头设置为允许访问标头。 https://i.imgur.com/8nS5SbH.png https://i.imgur.com/A1E4Mhh.png

任何帮助将不胜感激!

【问题讨论】:

标签: angular typescript rest


【解决方案1】:

这是因为您需要在后端的access-control-expose-headers 参数上添加标题名称。

例如,如果您想公开一个名为“my-custom-header”的标头,那么您需要在后端将此标头添加到access-control-expose-headers 参数,如

response.set({
 "my-custom-header": 'custom value',
 "access-control-expose-headers": "my-custom-header"
})

希望这会奏效

【讨论】:

    猜你喜欢
    • 2020-10-27
    • 1970-01-01
    • 2011-08-02
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    • 1970-01-01
    • 2018-11-26
    • 1970-01-01
    相关资源
    最近更新 更多