Spring @requestBody
页面提交请求参数有两种,一种是form格式,一种是json格式
jQuery的$.post方法虽然也可以传递json格式数据,但实际上是用的form格式提交,jquery会帮你把json转成form格式提交后台
所以其实可以通过$.post,$.get来提交json格式,让jquery帮你转换,可是如果后端用restful,那就只能用以下方法解决
<script type="text/javascript"> $(document).ready(function(){ var saveDataAry=[]; var data1={"userName":"test","address":"gz"}; var data2={"userName":"ququ","address":"gr"}; saveDataAry.push(data1); saveDataAry.push(data2); $.ajax({ type:"PUT", url:"/user", dataType:"json", contentType:"application/json;charset=utf-8", data:JSON.stringify(saveData), success:function(data){ } }); }); </script>
后台接收:
@RequestMapping(value = "user", method = RequestMethod.PUT ) @ResponseBody public void saveUser(@RequestBody List<User> users) { userService.batchSave(users); }