首页 > 解决方案 > Axios 发布问题和 .Net Core

问题描述

我无法使用 Axios Post 调用成功完成 ReactJs,在该调用中数据被传递到我的 asp.net core 2.2 web api。只有当所有复合词(姓氏)属性都被连字符连接时,调用才会成功到达我的 API 端点,但是数据没有通过。只有单个单词(公司)属性接收数据。我相信这是因为连字符的属性不再匹配。但是,如果属性没有连字符,我会收到 400 错误或 415 错误。当通过邮递员发送呼叫时,所有属性都是帕斯卡大小写的,我根本没有任何问题。

我已经尝试了很多方法来让它在没有解决方案的情况下工作。我已将Json属性添加到我的 .net 对象属性中。我已将属性修改为也有连字符。我尝试在对单个单词的反应中更改我的两个属性并将它们与Json属性 ei createdvs匹配CreatedDate。我还将PostMan调用中使用的完全相同的 json 对象粘贴到我的 post 方法中,该对象是成功的。

.net 帖子

[HttpPost]
[Route("AddThreshold", Name = "AddThreshold")]
public ActionResult<ThresholdDTO> AddThreshold([FromBody] ThresholdDTO threshold)
{
    var thr = _thresholdService.AddThreshold(threshold);

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

    return Ok(threshold);
}
  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();

    console.log(location);

    let threshold = JSON.stringify({
      "Threshold-Id": "",
      Company: company.toString(),
      "Threshold-Type": thresholdType.toString(),
      "Threshold-Value": thresholdValue.toString(),
      "Account-Number": account.toString(),
      "Location-Number": location == "undefined" ? "" : location.toString(),
      "Expected-Amount": expectedAmount.toString(),
      "Created-Date": formatted_date.toString(),
      "Created-By": "System",
      "Updated-Date": "1900-00-00 00:00:00.000",
      "Updated-By": ""
    });

    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);
      });
  };
AccountNumber: "87605177 (CenturyLink)"
Company: "Deere Employees Credit Union"
CreatedBy: "System"
CreatedDate: "2019-10-27 16:1:51"
ExpectedAmount: "90000"
LocationNumber: "5000"
ThresholdId: ""
ThresholdType: "Invoice Processing"
ThresholdValue: "10"
UpdatedBy: ""
UpdatedDate: "1900-00-00 00:00:00.000"

上面的对象正是邮递员所成功的,但是我400在使用 axios 时收到了一个代码

标签: reactjsaxiosasp.net-core-2.0

解决方案


问题是我的 UpdatedDate 值没有 Json 属性可接受的格式,导致整个帖子失败。


推荐阅读