javascript - 从 Web 调用时 Apiary API 端点返回空数据

标签 javascript reactjs axios apiary.io apiary

我在 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"
}
]

我有一个 React 应用程序,我在 componentWillMount 中调用的函数中使用 axios 访问这个端点:

  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);
  });

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

再次感谢,Mayank!

关于javascript - 从 Web 调用时 Apiary API 端点返回空数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42618429/

相关文章:

javascript - Vue js 2 和 Axios 发布请求 - 表单

javascript - 将脚本从请求更改为 axios - 记录待处理的 promise

javascript将变量从子级传递给父级

javascript - 使用BrowserRouter,如何清除注销历史记录

javascript - 使用 Sails.js 如何制作/users/me 与关联一起使用的路线?

node.js - 我需要将用户输入作为获取请求的一部分提交给 unsplash 服务器

javascript - 如何访问具有 json 字符串的 javascript 对象中的特定键值对

javascript - 如何使用 Antd InternalFormInstance 验证表单而不显示 UI 错误

reactjs - React.lazy 不在生产模式下工作

javascript - 用material-ui react searchkit(分页被禁用)