Angular 从http post请求体提取数据';s的反应
我有这个http post请求响应。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(
我想在不使用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)})除了这个问题,你为什么不想使用map呢?因为如果我使用map,我就无法获得res.status!事实上,你可以绘制所有东西,包括状态是的,没错,我已经完成了!但我无法从响应的正文部分提取身份验证令牌!我该怎么做?你有plunker吗?@ShoaibIqbal正如其他人所说的,你用.json()函数解析你的响应。设bodyRes=x.json();现在,您可以从bodyRes.auth中获取身份验证令牌_token@LLai,正是:)