使用axios POST时出现错误 [英] Getting error when using axios POST

查看:352
本文介绍了使用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'
}

我通过以下方式致电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);
});

我从服务器收到一个500错误.调用同一服务器的其他应用程序也可以正常工作.我什至在邮递员中试用了它,而且效果很好. axios我在做什么错了?

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?

推荐答案

很遗憾,这是有根据的猜测,因为未提供MCVE.

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

有关axios,请参见文档:

See the documentation for axios:

默认情况下,axios将JavaScript对象序列化为JSON.要改为以application/x-www-form-urlencoded格式发送数据,可以使用以下选项之一.

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.

您正在设置Content-Type标头,该标头声称您正在发送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.

这篇关于使用axios POST时出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆