[英]res.json() is a not a function in HttpClient Angular 2
之前我使用的是Angular Http
模塊,而res.json()
方法res.json()
工作得很好。 我最近嘗試過HttpClient
但是res.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()
而你自己不需要這樣做。 這是commit的引用:
JSON是假定的默認值,不再需要顯式解析
請參閱角度4中的HTTP和HTTPClient之間的差異? 更多細節。
由於HttpClient本身添加了res.json(),這意味着它隱式調用此函數,因此,顯式或手動調用它是毫無價值的。 所以沒有必要添加這個功能。 希望這有效,而不是......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.