我花了好几个小时试图弄清楚如何在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)
);
}
答案 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)
);
}