0

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

在役:

@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)

   );
}
4

1 回答 1

1

实际上,在您的情况下,您需要返回响应对象本身,而不仅仅是有效负载。

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

  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)

   );
}
于 2016-03-27T11:32:43.543 回答