res.json()在HttpClient Angular 2中不是函数 [英] res.json() is a not a function in HttpClient Angular 2

查看:68
本文介绍了res.json()在HttpClient Angular 2中不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前使用的是Angular Http模块,方法res.json()可以正常工作. 我最近尝试过HttpClient,但是res.json()似乎没有用.只有使用res的作品,一些人才能告诉我http客户端中发生了什么变化.

I was using Angular Http module before and the method res.json() used to work fine. I have recently tried HttpClient but then the res.json() dosen't seem to work . only using res works can some one tell me what changed has happened in the http client.

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客户端,因为新的拦截器可以欢迎指针了,谢谢

I switched to http client because of the new interceptor can pointers are welcomed thanks

推荐答案

是的,这是因为默认情况下,新的http客户端会隐式调用res.json(),而您无需自己手动进行操作.这是提交的引用:

Right, that's because new http client by default calls res.json() implicitly and you don't need to that manually yourself. Here is the quote from commit:

JSON是默认设置,不再需要显式解析

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

有关更多详细信息,请参见 HTTP和HTTPClient在角度4中的区别?.

See Difference between HTTP and HTTPClient in angular 4? for more details.

这篇关于res.json()在HttpClient Angular 2中不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆