简体   繁体   English

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

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

I have a mock API in Apiary where if I hit my endpoint with cURL or just in the browser, it returns: 我在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"
}
]

I have a React app where I'm hitting this endpoint with axios in a function that is called in componentWillMount : 我有一个作出反应的应用程序在那里我打这个端点与一个被称为函数爱可信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);
    });
  }

The console.log I get a status 200 in the response, but data is null. console.log我在响应中获得状态200,但data为空。 What am I doing wrong? 我究竟做错了什么?

Thanks to Mayank Shukla for the answer in the comments. 感谢Mayank Shukla在评论中的回答。 The key is to use the native axios GET method, like so: 关键是使用本机axios GET方法,如下所示:

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

Because the native GET has all the properties built in. In my example I hadn't defined all necessary properties. 因为本机GET具有内置的所有属性。在我的示例中,我没有定义所有必需的属性。

Thanks again, Mayank! 再次感谢Mayank!

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

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