前台ajax传数组,后台java接收
后端
//添加 @RequestMapping(value = "checkChoise") @ResponseBody ResultJson checkChoise(@RequestBody List<String> arr) { ResultJson resultJson = new ResultJson(); System.out.println("str:"+arr); resultJson.setStatus(0); resultJson.setMessage("添加成功!"); resultJson.setData(null); return resultJson; }
js
//某个角色函数 function comfirm(){ //获取checkbox[name='check']的值 var arr = new Array(); $("input:checkbox[name='check']:checked").each(function(i){ arr[i] = $(this).val(); }); $.ajax({ type: "post",//注意不能用get dataType: 'json', url: "/checkChoise", contentType: 'application/json',//这个必须是这个格式 data: JSON.stringify(arr),//前台要封装成json格式 success: function (result) { if (result.status == 0) { } if (result.message != null) { layer.msg(result.message) } }, error: function (XMLHttpRequest, textStatus, errorThrown) { layer.msg('{"status":"' + XMLHttpRequest.status + '","readyState":"' + XMLHttpRequest.readyState + '","textStatus":"' + textStatus + '","errorThrown":"' + errorThrown + '"}') } }) }