gpt4 book ai didi

angular - res.json() 不是 HttpClient Angular 2 中的函数

转载 作者:太空狗 更新时间:2023-10-29 18:06:51 25 4
gpt4 key购买 nike

我之前使用的是 Angular Http 模块,方法 res.json() 过去工作正常。我最近尝试了 HttpClientres.json() 似乎不起作用。仅使用 res 就可以有人告诉我 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客户端,欢迎指点,谢谢

最佳答案

是的,这是因为默认情况下新的 http 客户端会隐式调用 res.json(),您不需要自己手动调用。这是来自提交的引述:

JSON is an assumed default and no longer needs to be explicitly parsed

有关详细信息,请参阅 Difference between HTTP and HTTPClient in angular 4?

关于angular - res.json() 不是 HttpClient Angular 2 中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45780518/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com