Axios发布问题和.Net核心

问题描述 投票:0回答:1

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

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

。net post

[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"

以上对象正是邮递员成功完成的任务,但使用axios时却收到了400代码

reactjs axios asp.net-core-2.0
1个回答
0
投票

问题是我的UpdatedDate值没有Json Attribute可接受的格式,导致整个发布失败。

© www.soinside.com 2019 - 2024. All rights reserved.