Angular HttpClient:将HttpResponse转换为Response [英] Angular HttpClient: Convert HttpResponse to Response

查看:110
本文介绍了Angular HttpClient:将HttpResponse转换为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 API

这篇关于Angular HttpClient:将HttpResponse转换为Response的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆