Getting error when using axios POST

2020-05-09 19:22发布

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条回答
Explosion°爆炸
2楼-- · 2020-05-09 20:00

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.

查看更多
登录 后发表回答