①Controller后台POST接口参数要加上[FromBody],如下:
[HttpPost]
public ApiBaseResult PostFromBody([FromBody]UserEntity user)
{
return ReturnApiBaseResult(user);
}
public class UserEntity
{
public int UserID { get; set; }
public string UserName { get; set; }
public string Sex { get; set; }
public int Age { get; set; }
}
②AJAX调用,数据类型:dataType: "json"
$(function () {
$('#btn').click(function () {
var obj = { UserID: "123456",
UserNam": "张三",
Sex: "男",
Age: 18 };
var jsonStr = JSON.stringify(obj);
$.ajax({
type: "Post",
url: "https://localhost:44302/api/User/Demo/PostFromBody",
data: jsonStr,
dataType: "json",
contentType: 'application/json; charset=utf-8',
success: function (result) {
alert(result.Data.Name)
},
error: function (xhr, type, errorThrown)
{
alert('..............')
}
});
});
});
③用ApiPost测试,返回结果:
④Controller已获得传来的参数: