.net core 后台接收 ajax/接口请求 参数

1 接收表单参数

(1)后台代码

        [HttpPost]
        public IActionResult FormParas([FromForm]string para1, [FromForm] string para2)
        {
            return Json(new { code = 0, msg = $"接收到的参数 para1:{para1},para2:{para2}" });
        }

(2) ajax请求代码

    $.ajax({
        type: post,
        url: /Test/FormParas,
        data: { para1: p1, para2: p2 },
        contentType: application/x-www-form-urlencoded,
        dataType: json,// 响应类型
        success: function (res) {
            console.log(res);
        },
        error: function () {
            alert(程序出错);
        },
        beforeSend: function () {
            // 加载loading框
        },
        complete: function () {
            // 关闭loading框
        }
    });

(3) 使用postman请求

.net core 后台接收 ajax/接口请求 参数

 

 2 frombody 接收参数

(1)后台代码

        public IActionResult FromBody([FromBody]RequestModel requestModel)
        {
            requestModel.CreateTime = requestModel.CreateTime.Value.AddHours(8);
            return Json(new { code = 0 ,msg="操作成功1"});
        }

(2)ajax请求代码

    $.ajax({
        type: post,
        url: /Test/FromBody,
        data: JSON.stringify({ Id: 1, name: name1, money: 10.21, CreateTime: new Date(2021-05-29 16:53:10)}),
        contentType: application/json,
        dataType: json, // 后台响应类型
        success: function (res) {
        },
        error: function () {
            alert(程序出错);
        },
        beforeSend: function () {
            // 加载loading框
        },
        complete: function () {
            // 结束loading框
        }
    });

(3)postman请求

.net core 后台接收 ajax/接口请求 参数

 

 3  接收list

(1)后台代码

        public IActionResult AcceptList([FromForm]string reqId, [FromForm] List<RequestModel> requestModel)
        {
            return Json(new { code = 0, msg = "操作成功2" });
        }

(2)ajax代码

    $.ajax({
        type: post,
        url: /Test/AcceptList,
        data: { reqId: id, requestModel: [{ Id: 2, name: id2, money: 22, CreateTime: new Date(2021-05-29 22:53:10) }, { Id: 1, name: id1, money: 11, CreateTime: new Date(2021-05-29 12:53:10) }] },
        contentType: application/x-www-form-urlencoded,
        dataType: json,
        success: function (res) {
            if (res.code == 0)
                alert(res.msg);
        },
        error: function () {
        },
        beforeSend: function () {
        },
        complete: function () {
        }
    });

(3)postman请求

.net core 后台接收 ajax/接口请求 参数

 

 (4)后台请求实体如下

.net core 后台接收 ajax/接口请求 参数

 

 

参考资源:

https://www.cnblogs.com/linJie1930906722/p/12505618.html

 

注:后台是 .net core 5.0 项目类型为 asp.net core mvc 

.net core 后台接收 ajax/接口请求 参数

上一篇:.net 6手撸Wsk框架之五:使用Autofac实现依赖注入


下一篇:现在购买阿里云服务器需要多少钱?不同预算可购买的配置汇总