Axios: 为什么我们必须使用查询字符串模块?

创建于 2017-03-04  ·  3评论  ·  资料来源: axios/axios

花了几乎一整天的时间试图弄清楚为什么 Axios 以错误的格式发送我的数据,直到我发现这个http://stackoverflow.com/questions/31756756/axios-http-client-how-to-construct-http-post- url-with-form-params。 解决方法是使用查询字符串模块。

axios.post(
  '/api/v1/auth/login',
  querystring.stringify({ // <-- this is what fixed it. JSON.stringify didn't help here
    user_id: this.state.user_id,
    password: this.state.password,
    _csrf: result.data.csrfToken
  }),
  {
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
.then((res) => {
  if (res.data && res.data) {
    console.log('here')
  }
})
.catch((err) => {
  console.log(err);
});

但是,我觉得 Axios 应该照顾到这一点。

最有用的评论

您可以使用params来指定查询字符串。

  // `params` are the URL parameters to be sent with the request
  // Must be a plain object or a URLSearchParams object
  params: {
    ID: 12345
  },

所有3条评论

自述文件中已经有说明,所以应该很容易找到答案:

https://github.com/mzabriskie/axios#using -applicationx-www-form-urlencoded-format

Axios 是否应该自动处理这不是我要回答的问题,但该库的主要重点是发送 JSON,因此我猜它针对该用例进行了优化。

哦,谢谢你的链接。 我以前没见过。

您可以使用params来指定查询字符串。

  // `params` are the URL parameters to be sent with the request
  // Must be a plain object or a URLSearchParams object
  params: {
    ID: 12345
  },
此页面是否有帮助?
0 / 5 - 0 等级