简体   繁体   English

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

[英]res.json() is a not a function in HttpClient Angular 2

I was using Angular Http module before and the method res.json() used to work fine. 之前我使用的是Angular Http模块,而res.json()方法res.json()工作得很好。 I have recently tried HttpClient but then the res.json() dosen't seem to work . 我最近尝试过HttpClient但是res.json()似乎没有用。 only using res works can some one tell me what changed has happened in the http client. 只有使用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')));

I switched to http client because of the new interceptor can pointers are welcomed thanks 我切换到http客户端,因为新的拦截器可以指针欢迎谢谢

Right, that's because new http client by default calls res.json() implicitly and you don't need to that manually yourself. 是的,这是因为默认情况下新的http客户端隐式调用res.json()而你自己不需要这样做。 Here is the quote from commit: 这是commit的引用:

JSON is an assumed default and no longer needs to be explicitly parsed JSON是假定的默认值,不再需要显式解析

See Difference between HTTP and HTTPClient in angular 4? 请参阅角度4中的HTTP和HTTPClient之间的差异? for more details. 更多细节。

As HttpClient itself adds res.json(), which means it implicitly calls this function due to which, it is worthless to call it explicitly or manually. 由于HttpClient本身添加了res.json(),这意味着它隐式调用此函数,因此,显式或手动调用它是毫无价值的。 So there is no need to add this function. 所以没有必要添加这个功能。 Hope this works, thanx... 希望这有效,而不是......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM