首页 > Web开发 > 详细

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

时间:2021-05-30 15:44:30      阅读:19      评论:0      收藏:0      [点我收藏+]

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请求

技术分享图片

 

 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请求

技术分享图片

 

 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请求

技术分享图片

 

 (4)后台请求实体如下

技术分享图片

 

 

参考资源:

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

 

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

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

原文:https://www.cnblogs.com/tomorrow0/p/14827235.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!