- 1.前端不做处理,
var roleIds = new Array(); $("input[name='uRole']:checked").each(function(index,element){ let roleId = $(element).attr("data-roId"); roleIds[index] = roleId; }); $.ajax({ type: "post", url: baseUrl + "/userManage/updateUserRole",//对应controller的URL async: true, dataType: 'json', data: { "array": roleIds }, success:
后端接收:
String[] array = req.getParameterValues("array[]"); if (!StringUtils.isEmpty(array)) for (String string : array) { System.out.println("直接获取传递的数组参数:"+string); } else System.out.println("直接获取传递的数组参数为空");
- 2.以json字符串形式传递到Java后端
$.ajax({ type: "post", url: baseUrl + "/userManage/updateUserRole",//对应controller的URL async: true, dataType: 'json', contentType : "application/json", data: JSON.stringify(roleIds),//json对象转化为json字符串 success: function (ret) { console.log(ret); }
后端接收:
@RequestMapping(value = "/updateUserRole") @ResponseBody public String updateUserRole(HttpServletRequest req,@RequestBody String[] roleIdsss) { if (!StringUtils.isEmpty(roleIdsss)) for (int i = 0; i < roleIdsss.length; i++) { String idsss = roleIdsss[i]; System.out.println("requestBody获取传递的json数组参数:" + idsss); } else System.out.println("requestBody获取传递的json数组参数为空");
两种post请求的content-type不同
- 后端使用java对象接收
前端js 传递json字符串,设置contentType:"application/json":
var dataObj = { "userName":username, "phone":phone, "id":uId } var dataJsonStr = JSON.stringify(dataObj); console.log(dataJsonStr); $.ajax({ type:"post", url:baseUrl+"/userManage/updateSyeUser",//对应controller的URL async:true, contentType : "application/json", dataType: 'json', data: dataJsonStr, success:function(ret){
后台java:需要使用@requestBody
@RequestMapping(value = "/updateSyeUser",method = RequestMethod.POST) @ResponseBody public Map<String,Object> updateSyeUser(@RequestBody SysUser sysUser){//
参考链接: