net core后台接收 Form ajax post get请求参数

net core后台接收 Form ajax post get请求参数:

Get方法直接string接收值

                $.ajax({
                  url: "/AjaxRequest/GetString?str=一句话&id=1",
                  type: 'get',
                  success:function(response){
                      alert(response);
                  }
                });
        [HttpGet]
        public IActionResult GetString(string str, int id)
        {
            IQueryCollection queryParameters = HttpContext.Request.Query;
            string qstr = queryParameters["str"];
            string qid = queryParameters["ID"];

            Console.WriteLine($"通过参数接收:str:{str},id:{id}");

            Console.WriteLine($"通过原生方式参数接收:str:{qstr},id:{qid}");
            return new JsonResult("调用成功");
        }

在这里插入图片描述

Post接收字符串

                $.ajax({
                  url: "/AjaxRequest/PostString",
                  dataType: 'json',
                  type: 'post',
                  data:{ str : "一句话",id : 1},
                  success:function(response){
                      alert(response);
                  }
                });
        [HttpPost]
        public IActionResult PostString(string str, int id)
        {
            IFormCollection queryParameters = HttpContext.Request.Form;
            string qstr = queryParameters["str"];
            string qid = queryParameters["ID"];

            Console.WriteLine($"通过参数接收:str:{str},id:{id}");

            Console.WriteLine($"通过原生方式参数接收:str:{qstr},id:{qid}");
            return new JsonResult("调用成功");
        }

在这里插入图片描述

Post—FromQuery

  • 跟上一个方法,前台请求未变,只是接收参数增加FromQuery。效果一样。
        [HttpPost]
        public IActionResult PostStringFromForm([FromForm]string str, [FromForm] int id)
        {
            IFormCollection queryParameters = HttpContext.Request.Form;
            string qstr = queryParameters["str"];
            string qid = queryParameters["ID"];

            Console.WriteLine($"通过参数接收:str:{str},id:{id}");

            Console.WriteLine($"通过原生方式参数接收:str:{qstr},id:{qid}");
            return new JsonResult("调用成功");
        }

在这里插入图片描述

  • 下述内容会用到的实体类:

    public class Person
    {
        public int? Id { get; set; }
        public string Name { get; set; }
    }

FromForm与FromBody的区别为

  • FromForm:前台Data传来为Json,未转义为字符。可以实体也可以单个参数来接收。
  • FromBody:前台Data传来为转义为Json字符串,用实体类来接收。

Post—实体类FromForm


                $.ajax({
                  url: "/AjaxRequest/PostModelFromForm",
                  dataType: 'json',
                  type: 'post',
                  data:{ Id : 1,Name : "PostModelFromForm"},
                  success:function(response){
                      alert(response);
                  }
                });
        [HttpPost]
        public IActionResult PostModelFromForm([FromForm]Person person)
        {
            Console.WriteLine($"通过参数接收:str:{JsonConvert.SerializeObject(person)}");

            return new JsonResult("调用成功");
        }

在这里插入图片描述

Post—实体类FromForm

  • 实体与参数分别赋值。
    在这里插入图片描述

Post—实体类FromBody

                $.ajax({
                  url: "/AjaxRequest/PostModelFromBody",
                  dataType: 'json',
                  contentType: 'application/json; charset=utf-8',
                  type: 'post',
                  data:JSON.stringify({ Id : 1,Name : "PostModelFromBody"}),
                  success:function(response){
                      alert(response);
                  }
                });
        [HttpPost]
        public IActionResult PostModelFromBody([FromBody]Person person)
        {
            Console.WriteLine($"通过参数接收:str:{JsonConvert.SerializeObject(person)}");

            return new JsonResult("调用成功");
        }

在这里插入图片描述

Post—dynamic动态类型

  • JS请求同Post—实体类FromBody
        [HttpPost]
        public IActionResult PostDynamic([FromBody] dynamic person)
        {
            string Id=person.Id;
            string Name=person.Name;
            Console.WriteLine($"通过参数接收:Id:{Id},Name:{Name},str:{JsonConvert.SerializeObject(person)}");

            return new JsonResult("调用成功");
        }

在这里插入图片描述

Post—JObject参数

  • JS请求同Post—实体类FromBody
        [HttpPost]
        public IActionResult PostJObject([FromBody] JObject person)
        {
            string Id = person["Id"].ToString();
            string Name = person["Name"].ToString();
            Console.WriteLine($"通过参数接收:Id:{Id},Name:{Name},str:{JsonConvert.SerializeObject(person)}");

            return new JsonResult("调用成功");
        }

在这里插入图片描述

模型绑定官方地址:https://docs.microsoft.com/zh-cn/aspnet/core/mvc/models/model-binding?view=aspnetcore-6.0

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在.NET Core 6 MVC项目中使用jQuery发起ajax post请求并携带数组参数,可以按照以下步骤进行操作: 1. 创建一个视图,包含一个表单和一个提交按钮。 ```html <form id="myForm" method="post"> <input type="text" name="name" /> <input type="text" name="age" /> <input type="button" id="btnSubmit" value="提交" /> </form> ``` 2. 编写jQuery代码,使用`$.ajax`方法发起post请求,并携带数组参数。 ```javascript <script> $("#btnSubmit").click(function () { var data = { "names": ["Tom", "Jerry"], "ages": [12, 16] }; $.ajax({ type: "POST", url: "/Home/PostData", data: data, success: function (result) { console.log(result); } }); }); </script> ``` 3. 在控制器中创建一个接收post请求的方法,并使用`[FromBody]`特性将请求体中的数据绑定到C#对象上。 ```csharp [HttpPost] public IActionResult PostData([FromBody]MyViewModel myViewModel) { // 处理请求数据 return Json(new { success = true }); } public class MyViewModel { public string[] Names { get; set; } public int[] Ages { get; set; } } ``` 注意事项: - jQuery的`$.ajax`方法中,`data`参数可以直接使用JavaScript对象,jQuery会自动将其转换为适当的格式。 - 在控制器方法中,需要使用`[FromBody]`特性将请求体中的数据绑定到C#对象上。 - 在控制器方法中,可以返回Json数据,使用`Json`方法即可。 以上就是.NET Core 6 MVC项目中使用jQuery发起ajax post请求并携带数组参数的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值