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

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

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);
});

我从服务器收到 500 错误.调用同一服务器的其他应用程序工作正常.我什至在 Postman 中试过这个,它有效.我在 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天全站免登陆