.NetCore mvc Ajax Post数据到后端

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

后端Controller中定义以下方法:

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

注意一定要有 [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

猜你喜欢

转载自www.cnblogs.com/zhaoyongkai/p/9436693.html