[axios]使用 json (body: raw) POST 数据时总是失败

2024-08-22 127 views
1

你好,我在向服务器发送数据时遇到了麻烦。我正在使用 react native 和 axios ver ^0.16.2

let input = {
  'longitude': -6.3922782,
  'latitude': 106.8268856,
  'content': 'uget - uget sampai kaki lemes',
  'pictures': []
}

  axios({
    method: 'POST',
    url,
    headers: {
      'Content-Type': 'application/json',
      'Authorization': this.state.headers.authorization
    },
    data: input
  })                                                                     
  .then((resultAxios) => {
    console.log('hasil axios', resultAxios)
  })

并且状态结果总是错误500。

如果我尝试使用邮递员发送数据,邮递员中一切都很好
,我设置

标头:{授权:''',内容类型:application/json}

主体 = 原始,JSON

如何解决这个问题?谢谢:)

回答

9

这不太可能是 Axios 的问题,所以您能否将其发送给GitterStackOverflow,并提供足够的信息来了解您遇到的问题?谢谢。

1

我也遇到了这个问题。当我执行 POST 并且服务器返回代码 500 时,我没有收到任何响应,尽管我在网络选项卡上看到了响应。这是 fiddle 演示:https://jsfiddle.net/8r3dh6hw/61/

2

试试这个..它将适用于通过 axios 发送的数据

   const params = {
              'longitude': -6.3922782,
              'latitude': 106.8268856
    };

    axios.post('http://localhost/react/submit.php', params, {
         headers: {
              'content-type': 'application/json',
         },
    })
6

标头:{'content-type':'application/json',}

你是我的英雄!

9

谢谢!它起作用了@gmkhussain

4

JS 的字很残忍,reposnseType 是直接声明的,而 content-type 必须放在 header 中。
谢谢老兄!

9

我也遇到了这个问题。我有一个对象,通过 Postman 发布时工作得很好,它完全是 Pascal 大小写,但除非我使用连字符连接属性,否则不会通过 Axios 发布。但是,我的 .net core 应用程序无法获取数据,因为属性不再匹配。我甚至在我的对象上创建了带连字符的 json 属性,但这不起作用。我也在对象上使用了 JSON.stringify,但没有成功。无论我做什么,我都无法成功执行发布并接收所有数据。

const handleAddSubmit = e => { e.preventDefault();

let current_datetime = new Date();
let formatted_date =
  current_datetime.getFullYear() +
  "-" +
  (current_datetime.getMonth() + 1) +
  "-" +
  current_datetime.getDate() +
  " " +
  current_datetime.getHours() +
  ":" +
  current_datetime.getMinutes() +
  ":" +
  current_datetime.getSeconds();

let threshold = JSON.stringify({
  ThresholdId: "",
  Company: company.toString(),
  ThresholdType: thresholdType.toString(),
  ThresholdValue: thresholdValue.toString(),
  AccountNumber: account.toString(),
  LocationNumber: location == "undefined" ? "" : location.toString(),
  ExpectedAmount: expectedAmount.toString(),
  CreatedDate: formatted_date.toString(),
  CreatedBy: "System",
  UpdatedDate: "1900-00-00 00:00:00.000",
  UpdatedBy: ""
});

Axios({
  method: "post",
  url: "https://localhost:44394/Threshold/AddThreshold/",
  data: threshold,
  headers: { "Content-Type": "application/json" }
})
  .then(function(response) {
    //handle success
    console.log(threshold);
    props.handleAddClose();
  })
  .catch(function(response) {
    //handle error
    console.log(threshold);
  });

};

我的.net核心方法如下所示: [HttpPost] [Route("AddThreshold", Name = "AddThreshold")] public ActionResultAddThreshold([FromBody] ThresholdDTO 阈值) { var thr = _thresholdService.AddThreshold(threshold);

        if (thr == null)
            return NoContent();

        return Ok(threshold);
    }

我还忘了说,我的 Chrome 开发工具中显示的数据全部为已提供。我还注意到,json 的属性不在引号内。这可能是原因吗,我不确定?我也不确定为什么这个问题似乎没有得到解决,但却被关闭了。

8

@Troy1b4 如果 json 负载出现在 devtools 中,则服务器出现错误。