天天看点

.net core3.1 mvc post 请求(ajxa)

问题

最近在做.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)
        {
        }
           

断点调试