简体   繁体   English

使用 axios POST 时出错

[英]Getting error when using axios POST

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

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.我从服务器收到500错误。 Other apps calling the same server works fine.调用同一服务器的其他应用程序工作正常。 I even tried this out in Postman and it works.我什至在 Postman 中尝试过这个,它有效。 What am I doing wrong in axios ?我在axios做错了axios

This is, sadly, an educated guess because a MCVE was not provided.遗憾的是,这是一个有根据的猜测,因为没有提供 MCVE。

See the documentation for axios:请参阅 axios 的文档

By default, axios serializes JavaScript objects to JSON.默认情况下,axios 将 JavaScript 对象序列化为 JSON。 To send data in the application/x-www-form-urlencoded format instead, you can use one of the following options.要改为以 application/x-www-form-urlencoded 格式发送数据,您可以使用以下选项之一。

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.您正在设置一个Content-Type标头,该标头声称您正在发送application/x-www-form-urlencoded数据,但您尚未执行文档建议的以该格式生成数据的任何操作。

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

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