.NetCore mvc Ajax Post数据到后端

时间:2023-03-09 17:44:23
.NetCore mvc Ajax Post数据到后端

在前端页面中,如果没有表单,想把复杂对象提交到后端,可使用以下方法

后端Controller中定义以下方法:

[HttpPost]
public int AddSolution([FromBody]Solution col)
{
if (col != null)
{
//你的操作
}
else
{
return -;
}
return ;
}

注意一定要有 [FromBody]

前端JS方法

function fn_addSolution() {
var data = {
TableName: "UserTable",
Name:"lisi",
DisplayName: "李斯",
}; $.ajax({
type: "POST",
url: "/Solution/AddSolution",
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
if (result)
}
});
}

注意其中的 contentType和dataType