Getting error when using axios POST

六眼飞鱼酱① 提交于 2019-12-20 07:43:47

问题


My url, headers and body data are defined as:

var headers = {
    'Authorization': 'Bearer 12345',
    'Content-Type': 'application/x-www-form-urlencoded'
  }

var data = {
  'password': '123456',
  'ver': '1',
  'time': '1534494857045'
}

I am calling axios by:

axios.post(url, data, headers)
.then(response => {
  console.log(response);
})
.catch(error => {
  if (error.response) {
    console.log(error.response.data);
    console.log(error.response.status);
    console.log(error.response.headers);
  } else if (error.request) {
    console.log(error.request);
  } else {
    console.log('Error', error.message);
  }
  console.log(error.config);
});

I am getting a 500 error from the server. Other apps calling the same server works fine. I even tried this out in Postman and it works. What am I doing wrong in axios?


回答1:


This is, sadly, an educated guess because a MCVE was not provided.

See the documentation for axios:

By default, axios serializes JavaScript objects to JSON. To send data in the application/x-www-form-urlencoded format instead, you can use one of the following options.

You are setting a Content-Type header which claims you are sending application/x-www-form-urlencoded data, but you haven't done any of the things that the documentation suggests to generate data in that format.



来源:https://stackoverflow.com/questions/51892937/getting-error-when-using-axios-post

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!