Angular HttpClient:将 HttpResponse 转换为响应 [英] Angular HttpClient: Convert HttpResponse to Response
问题描述
在使用 Http
时,我们从 @angular/http
库中获取响应类型为 Response
,类似地使用 res 转换为 JSON.json()
While using Http
we get response type as Response
from @angular/http
library, similarly converting to JSON using res.json()
但是通过使用来自 @angular/common/http
的 HttpClient
我们通过使用 observe
得到直接的 JSON 或 HttpResponse
>
But by using HttpClient
from @angular/common/http
we get direct JSON or HttpResponse
by using observe
是否有任何解决方法可以将类型从 Response
转换为 HttpResponse
或以任何其他方式从 HtpClient
获取响应,以便 .json() 会起作用吗?
Is there any work around to convert type from Response
to HttpResponse
or any other way to get response from HtpClient
so that .json() will work?
推荐答案
http 客户端默认会隐式调用 res.json(),您无需自己手动将响应类型设置为 responseType代码> 到
json
http client by default calls res.json() implicitly and you don't need to that manually yourself and set the respond type to responseType
to json
JSON is an assumed default and no longer needs to be explicitly parsed
这篇关于Angular HttpClient:将 HttpResponse 转换为响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!