reactjs - Axios 发布问题和 .Net Core
问题描述
我无法使用 Axios Post 调用成功完成 ReactJs,在该调用中数据被传递到我的 asp.net core 2.2 web api。只有当所有复合词(姓氏)属性都被连字符连接时,调用才会成功到达我的 API 端点,但是数据没有通过。只有单个单词(公司)属性接收数据。我相信这是因为连字符的属性不再匹配。但是,如果属性没有连字符,我会收到 400 错误或 415 错误。当通过邮递员发送呼叫时,所有属性都是帕斯卡大小写的,我根本没有任何问题。
我已经尝试了很多方法来让它在没有解决方案的情况下工作。我已将Json
属性添加到我的 .net 对象属性中。我已将属性修改为也有连字符。我尝试在对单个单词的反应中更改我的两个属性并将它们与Json
属性 ei created
vs匹配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 时收到了一个代码
解决方案
问题是我的 UpdatedDate 值没有 Json 属性可接受的格式,导致整个帖子失败。
推荐阅读
- c++ - 如何使用段树查找数组中的前 2 个最小整数
- checkstyle - 使用 checkstyle 为 lombok 注释抛出错误
- r - dplyr tibbles 中是否可以有表达式?
- javascript - 在 Firefox Webextension Contentscript 中使用 fontawesome
- algorithm - 具有常见重复序列的核苷酸的高效数据存储
- c++ - 手动查找 UV 点
- javascript - 如何通过 Cockpit api 进行更新
- reactjs - 将 `eslint-plugin-jsx-a11y` 与 `styled-components` 一起使用
- python - 使用 Django 客户端测试多个复选框
- matlab - 使用用户定义函数的卷积核。如何处理负像素值?