您的位置:首页 > Web前端 > jquery > 正文

ajax 向后台传递数组参数

更多 时间:2015-7-6 类别:Web前端 浏览量:1518

ajax 向后台传递数组参数

ajax 向后台传递数组参数

方式一、使用 traditional:true 属性

 

实例1

  • 
    $.ajax({
        type: "POST",
        url: url,
    
       traditional:true,
       data:{ gender:[0,1] },
       dataType: "json",
        async:false
       });
    
    
    		
  •  

    实例2

  • 
            var _list = [];
    
            for (var i = 0; i < checkedRow.length; i++) {
                _list[i] = checkedRow[i].ID;
            }
    
            $.ajax({
                url: 'url',
                data: { "selectedIDs": _list },
                dataType: "json",
                type: "POST",
                traditional: true,
                success: function (responseJSON) {
                    // your logic
                    alert('Ok');
                }
            });
    
    		
  • 后台代码

  •  
  • 
    public ActionResult SetCallBackStatus(List<int> selectedIDs)
            {
                string result = "ok";
                string errMsg = "";
    
                return this.JsonFormat(new { result = result, errMsg = errMsg });
            }
    
    		
  •  

    方式二

     

  •  
  • 实例1
  •  
  •  
  • 
            var _list = {};
    
            for (var i = 0; i < checkedRow.length; i++) {
                _list["selectedIDs[" + i + "]"] = checkedRow[i].ID;
            }
    
            $.ajax({
                url: 'url',
                data: _list,
                dataType: "json",
                type: "POST",
                success: function (responseJSON) {
                    // your logic
                    alert('Ok');
                }
            });
    
    		
  • 后台代码

  •  
  • 
    public ActionResult SetCallBackStatus(List<int> selectedIDs)
            {
                string result = "ok";
                string errMsg = "";
    
                return this.JsonFormat(new { result = result, errMsg = errMsg });
            }
    
    		
  •  

    实例2、自定义的类数组

  •  
  • 
        // 自定义Person类
        public class Person
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
        }
    
    		
  •  
  •  
  • 
            var _list = {};
    
            for (var i = 0; i < checkedRow.length; i++) {
                _list["selectedIDs[" + i + "].FirstName"] = checkedRow[i].FirstName;
                _list["selectedIDs[" + i + "].LastName"] = checkedRow[i].LastName;
            }
    
            $.ajax({
                url: 'url',
                data: _list,
                dataType: "json",
                type: "POST",
                success: function (responseJSON) {
                    // your logic
                    alert('Ok');
                }
            });
    
    				
  • 后台代码

  •  
  • 
            // 后台Action
            public ActionResult SetCallBackStatus(List<Person> selectedIDs)
            {
                string result = "ok";
                string errMsg = "";
    
                return this.JsonFormat(new { result = result, errMsg = errMsg });
            }
    
    		
  •  

    标签:jquery ajax