简体   繁体   中英

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. 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')));

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

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 is an assumed default and no longer needs to be explicitly parsed

See Difference between HTTP and HTTPClient in angular 4? 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. So there is no need to add this function. Hope this works, thanx...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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