headers 回复 http.get

headers response in http.get

我花了几个小时试图弄清楚如何在 http.get 请求后获得 headers 响应 x-total-objects 和状态代码,我有这个 class 服务,我需要访问这些属性来对我的结果进行分页

在役:

@Injectable()
export class WPCollections{

  constructor(private http: Http){  }

  fetch(url){
     let headers = new Headers({ 'Content-Type': 'application/json' });
     let options = new RequestOptions({ headers: headers });

     return this.http.get(url, options).map(res => res.json());
  }
}

在组件中:

@Input() args;
posts = new Array<any>();
service: Observable<any>;

constructor(private wp: WPCollections) { }

fetchData(args){
   this.service = this.wp.fetch(args);
   this.service.subscribe( 
     collection=>{
         this.posts = collection;
     },
     err => this.onError(err)

   );
}

事实上,在您的情况下,您需要 return 响应对象本身,而不仅仅是负载。

为此,您要删除地图运算符:

  fetch(url){
     let headers = new Headers({ 'Content-Type': 'application/json' });
     let options = new RequestOptions({ headers: headers });

     return this.http.get(url, options);
  }
}

在你的组件中:

@Input() args;
posts = new Array<any>();
service: Observable<any>;

constructor(private wp: WPCollections) { }

fetchData(args){
   this.service = this.wp.fetch(args);
   this.service.subscribe( 
     response=>{
         this.posts = response.json();
       var headers = response.headers;
     },
     err => this.onError(err)

   );
}