ajax向后台传递参数数组

前台数据:

  数组

  1-->  直接定义

  2-->  获取页面数据 填充数组(获取的是select中的option数据)

var data = document.getElementById('role');    
var arrs =new Array();
for(var i=0;i<data.options.length;i++){            
    arrs.push(data.options[i].value);                            
}

  ajax请求  

  一开始注意增加属性 traditional:true, 没有变化

var userId = $("#user").val();
if(arrs.length!=0){
    $.ajax({
        type: "POST",
        url: "/userRole/saveOrUpdate",                  
        data: {"userId":userId,"arr":arrs},
        traditional:true,
        async: true,
        dataType:"json",
        success: function(data){
        },
        error: function(){
        }
    });
}

  后台接受

public String saveOrUpdate( String userId, HttpServletRequest request) {
       String[] arr=request.getParameterValues("arr");
    System.out.println(arr);
}

  

猜你喜欢

转载自www.cnblogs.com/cmz-32000/p/11584794.html