【问题标题】:http get request returns different values in curl than it does in ionic2/angular2http get 请求在 curl 中返回的值与在 ionic2/angular2 中不同
【发布时间】:2017-02-16 15:14:51
【问题描述】:

当我像这样使用 curl 发出 get 请求时:

curl https://api.backand.com:443/1/objects/todos?AnonymousToken=my-token

我得到了正确的信息:

{"totalRows":2,"data":[{"__metadata":{"id":"1","fields":{"id":{"type":"int","unique":true},"content":{"type":"string"}},"descriptives":{},"dates":{}},"id":1,"content":"the first todo!"},{"__metphilipphpphilipphilip"}]}

但是当我像这样在 angular 2 中执行相同的请求时:

this.http.get('https://api.backand.com:443/1/objects/todos?AnonymousToken=my-token').map(res => {
  return res.json();
});;

我返回了一个名为 observable 的奇怪对象,它看起来像这样:

为什么请求不能以角度工作?

【问题讨论】:

  • return res.json().data; 试试看
  • 您在哪里以及如何订阅它?
  • 谢谢,尝试添加数据,但不起作用。我得到同样的东西。你是什​​么意思订阅?我只是想做一个简单的获取请求。

标签: angular typescript ionic2 rxjs ionic3


【解决方案1】:

我对Curl一无所知,但是Angular 2中的HTTP调用返回observables,所以你需要使用RxJS方法对它们进行操作。为了获得响应,您需要订阅它以便观察返回的值。

 this.http.get('https://api.backand.com:443/1/objects/todos?AnonymousToken=my-token')
    .map(res => res.json())
    .subscribe(
      data => this.myData = data,
      err => this.logError(err),
      () => console.log('Request Complete')
    );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-25
    • 1970-01-01
    • 1970-01-01
    • 2016-11-04
    • 2022-09-30
    • 2017-06-23
    • 2017-06-07
    相关资源
    最近更新 更多