Angular 从http post请求体提取数据';s的反应

Angular 从http post请求体提取数据';s的反应,angular,http,Angular,Http,我有这个http post请求响应。 我想在不使用out的情况下使用.map访问此响应的正文。任何帮助 你可以简单地提供这样的服务 getResponse() { return this.http.get(......).map(x => x.json()); } this.customService.getResponse.subscribe(res => { console.log(res) }) 然后,您可以从这样的组件获得此响应 getResponse(

我有这个http post请求响应。


我想在不使用out的情况下使用.map访问此响应的正文。任何帮助

你可以简单地提供这样的服务

getResponse() {
    return this.http.get(......).map(x => x.json());
  }
this.customService.getResponse.subscribe(res => {
  console.log(res)
})
然后,您可以从这样的组件获得此响应

getResponse() {
    return this.http.get(......).map(x => x.json());
  }
this.customService.getResponse.subscribe(res => {
  console.log(res)
})

不使用地图:

getResponse() {
    return this.http.get(......);
}

this.customService.getResponse.subscribe(res => {
  console.log(res.json())
})
但是,最佳实践是使用map


我不想使用带有get请求的.map?我想简单地将响应从服务转到调用它的位置,然后在那里访问响应的主体。如果您删除.map函数,那么响应将直接在subscribe(res)的成功回调中返回。如果您不想在服务内部使用map,那么只需将
x=>x.json
移动到组件。比如
subscribe(res=>{const body=res.json();console.log(body)})