Angular HttpClient:将HttpResponse转换为Response [英] Angular HttpClient: Convert HttpResponse to Response
问题描述
使用Http
时,我们从@angular/http
库获得响应类型为Response
,类似地使用res.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
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
到
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转换为Response的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!