如何在不将其转换为字符串或json的情况下访问Angular 2 http响应主体? [英] How can I get access to the Angular 2 http response body without converting it to string or json?

查看:84
本文介绍了如何在不将其转换为字符串或json的情况下访问Angular 2 http响应主体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将REST响应复制到Blob中,但是由于在响应对象中尚未实现blob()arrayBuffer(),因此无法执行某些操作.响应主体是一个私有变量.

I would like to copy a REST response into a blob but I am unable to do some because blob() and arrayBuffer() have not yet been implemented in the Response Object. The Response Body is a private variable.

...
return this.http.get(url, {params: params, headers: headers})
     .map(res => {   
        // can't access _body because it is private
        // no method appears to exist to get to the _body without modification             
        new Blob([res._body], {type: res.headers.get('Content-Type')});
     })
     .catch(this.log);
...

在实现这些方法之前,有没有可以使用的解决方案?

Is there a solution I can use until these methods get implemented?

推荐答案

有一种更简单的解决方案,可以以字符串形式访问正文,而我在任何地方都没有看到它:

There's a much simpler solution to accessing the body as a string which I haven't seen documented anywhere:

let body = res.text()

这篇关于如何在不将其转换为字符串或json的情况下访问Angular 2 http响应主体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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