Jquery ajax传递数组

来源:互联网 发布:linux bash profile 编辑:程序博客网 时间:2024/04/29 15:37

前言

使用jquery ajax传递参数的时候,通常都会使用字符串,遇到复杂对象时也是会将数据转换成Json,然后在使用json2等工具转换成字符串,然后再到后台对json字串进行解析。不过在传递数组的时候一直很纠结,当然把数组转换成字符串或json都可以。这里介绍一种ajax传递数组的方法,感觉代码稍简单一些,没有编码 和解码的操作了。

前台

function batchDelete(){var ids = new Array();$('input[name="roleCheck"]:checked').each(function(){   // alert($(this).val())ids.push($(this).val());       });$.ajax({             url : '${ctx}/role/batchDeleteRole.do',             cache : false,             type : 'post',             dataType : 'html',             async : false,             contentType : "application/x-www-form-urlencoded;charset=utf-8",             data : {                 'ids' : ids           },             success : function(htmlRet) {            //alert("xxx1")                       }          })          refreshForm();}

后台

@RequestMapping("/batchDeleteRole")public void batchDeleteRole(HttpServletRequest request,HttpServletResponse response){String[] roleids = request.getParameterValues("ids[]");String ret = "0";/**/for(String roleid:roleids){ roleService.deleteRole(roleid);}// ret = "1";ResponseUtil.writeResponseStr(response, ret);}



0 0
原创粉丝点击