繁体   English   中英

当从Web调用时,蜂房API端点返回空数据

[英]Apiary API endpoint returning null data when called from web

我在Apiary中有一个模拟API,如果我用cURL或仅在浏览器中点击我的端点,它会返回:

[
{
  "id": 1,
  "user_id": 1,
  "status": "Review"
  "name": "New York Songwriter",
  "negotiable": true,
  "description": "",
  "created_at": "2015-02-09T02:29:58 +08:00",
  "modified_at": "2015-02-17T05:30:17 +08:00"
},
{
  "id": 2,
  "user_id": 1,
  "status": "Accepted"
  "name": "Licensing Contract",
  "negotiable": false,
  "description": "Third party licensing agreement",
  "created_at": "2015-02-09T02:29:58 +08:00",
  "modified_at": "2015-02-17T05:30:17 +08:00"
}
]

我有一个作出反应的应用程序在那里我打这个端点与一个被称为函数爱可信componentWillMount

  componentWillMount() {
    this.loadItems();
  }

  loadItems() {
    var self = this;
    axios({
      url: ENDPOINT,
      method: 'get',
      responseType: 'json'
    })
    .then(function(r) {
      console.log(r);
      var d = r.data;
      self.setState({
        agreements: d
      });
    })
    .catch(function(r) {
      console.log(r);
    });
  }

console.log我在响应中获得状态200,但data为空。 我究竟做错了什么?

感谢Mayank Shukla在评论中的回答。 关键是使用本机axios GET方法,如下所示:

axios.get(ENDPOINT)
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

因为本机GET具有内置的所有属性。在我的示例中,我没有定义所有必需的属性。

再次感谢Mayank!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM