問題
最近在做.net core3.1 mvc post請求時,發現post參數資料一直擷取不到,經過多方查閱發現請求時要加 contentType: "application/x-www-form-urlencoded",并且背景要加[FromForm]。
示例如下:
前端
$.ajax({
url: "/DataMap/GetData",
type: "POST",
contentType: "application/x-www-form-urlencoded",
async: true,
dataType: "json",
data: { taskPage: { StartYear: startYear, EndYear: endYear, Page: pagenum, Limit:pagelimit,Types: types, MultiPolygonJson: multiPolygonJson, PointJson: pointJson } },
success: function (data) {
console.log(data);
}, error: function (XMLHttpRequest, textStatus, errorThrown) {
}
});
後端
[HttpPost]
public async Task<JsonResult> GetData([FromForm] TaskPage taskPage)
{
}