【问题标题】:get headers from get Http request in Angular从 Angular 中的获取 Http 请求中获取标头
【发布时间】:2018-05-02 12:39:49
【问题描述】:

我正在调用获取 blob 数据的 API。

后端还向我发送标题中的文件名。

我的实际问题是我无法从 api 获取标题。

这是我的service.ts

public openFile(path) {
  let url='/download/';
  let pathFile= new HttpParams().set('pathFile', path);
  return this.httpClient.get(url,{params:pathFile, responseType: 'blob' });

并在 component.ts 中调用该服务。当我尝试打印 res.headers 时,我在控制台中未定义。

openFile(path){
  this.creditPoliciesService.openFile(path).toPromise().then (data => {
    console.log("dataaaaaa",data.headers); // undefined
    var blob = new Blob([data], {type: 'application/pdf'}); 
    if (window.navigator && window.navigator.msSaveOrOpenBlob) {
      window.navigator.msSaveOrOpenBlob(blob);
    }
    else {
      var fileURL = URL.createObjectURL(blob); 
      window.open(fileURL);
    }
  });
}

在开发工具管理员中,我在响应标头中获取信息,但我无法在响应变量中找到它们。

【问题讨论】:

标签: angular typescript http-headers httprequest angular-services


【解决方案1】:

传递值为“response”的观察键以获得完整的响应

getData() {
 this.http.get(this.url, { observe: 'response' }).subscribe(res => {
   this.headerProperty = res.headers.get('property name here');
 });
}

【讨论】:

  • 我应该导入一些模块吗?
  • 我在我的请求中添加了{ observe: 'response' },但我没有得到我在浏览器开发工具中看到的所有标题响应项
  • 我通过从前端获取信息(而不是 http 请求)解决了我的问题。但我在 github 上找到了这个,它可能会帮助更多人github.com/angular/angular/issues/13226
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-24
  • 1970-01-01
  • 1970-01-01
  • 2015-05-18
  • 1970-01-01
相关资源
最近更新 更多