http.get中的标头响应

时间:2016-03-27 10:58:03

标签: http get angular wp-api

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

在服务中:

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

   );
}

1 个答案:

答案 0 :(得分: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)

   );
}