【问题标题】:headers response in http.gethttp.get 中的标头响应
【发布时间】:2016-03-27 10:58:03
【问题描述】:

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

服务中:

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

   );
}

【问题讨论】:

    标签: http get angular wp-api


    【解决方案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)
    
       );
    }
    

    【讨论】:

      猜你喜欢
      • 2018-11-02
      • 1970-01-01
      • 2018-05-18
      • 2017-12-19
      • 2016-03-03
      • 2019-01-20
      • 2019-08-22
      • 1970-01-01
      • 2016-05-06
      相关资源
      最近更新 更多