我之前使用的是Angular Http
模块,res.json()
使用的方法工作得很好。我最近尝试过HttpClient
,但是res.json()
似乎不起作用。只有使用res
works才能告诉我http客户端发生了什么变化。
return this.client.get('https://swapi.co/api/people/1/')
.map((res:Response) => {
return res.json(); // using maps to filter data returned form the http call this json dosn't work with http client
}).map(data => {
return data; // using maps of maps to filter data returned form the map
}).flatMap((jedi) => this.http.get(jedi['homeworld'])
.map(res => {
return res.json().name; // using flat maps to combine data returned from two observables into one
}).catch((error:any) => Observable.throw(error.json().error || 'Server error')));
我切换到http客户端,因为新的拦截器可以指针是受欢迎的谢谢
发布于 2017-08-20 09:26:31
是的,这是因为新的http客户端默认情况下会隐式调用res.json()
,您不需要自己手动执行此操作。下面是commit中的一句话:
JSON是假定的默认值,不再需要显式解析
有关更多详细信息,请参阅Difference between HTTP and HTTPClient in angular 4?。
发布于 2018-05-29 06:03:07
由于HttpClient本身添加了res.json(),这意味着它隐式地调用此函数,因此显式或手动调用它是没有价值的。因此,不需要添加此函数。希望这行得通,谢谢...
https://stackoverflow.com/questions/45780518
复制相似问题