Angular 2:如何访问HTTP响应正文? [英] Angular 2: How to access an HTTP response body?
本文介绍了Angular 2:如何访问HTTP响应正文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Angular 2中编写了以下代码:
I wrote the following code in Angular 2:
this.http.request('http://thecatapi.com/api/images/get?format=html&results_per_page=10').
subscribe((res: Response) => {
console.log(res);
})
当我打印响应时,我进入控制台:
When I print the response I get in console:
我想在代码中访问响应中的body字段。 'body'字段以下划线开头,这意味着它是一个私有字段。当我将其更改为'console.log(res._body)'时出现错误。
I want to have access in the code to body field in the response. The 'body' field starts with an underscore, which means that it's a private field. When I change it to 'console.log(res._body)' I got an error.
你知道任何可以帮助我的吸气功能吗?
Do you know any getter function that can help me here?
推荐答案
两者 请求
和 响应
扩展 正文
。
要获取内容,请使用 text()
方法。
this.http.request('http://thecatapi.com/api/images/get?format=html&results_per_page=10')
.subscribe(response => console.log(response.text()))
这篇关于Angular 2:如何访问HTTP响应正文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文