第一种方法直接用serialize()方法
function insert(){ $.ajax({ type:"POST", url:"${pageContext.request.contextPath}/order/insert", data : $("#fom").serialize(), success :function (res) { console.log(res); error:function () { } });}
后台springmvc用对象参数接收 可以自动转换为对象,需要注意的就是form表单中的name要和对象中的参数名相同
@RequestMapping(value = "/insert",method = RequestMethod.POST) public String insert( Order order){ int result=this.orderSerivce.insert(order); if(result==1){ System.out.println("添加失败"); return "101"; } return "100"; }
第二种是用JSON.stringify()将json对象转化为json对象的字符串传递
function insert(){ $.ajax({ type:"POST", url:"${pageContext.request.contextPath}/user/insert", async:false, data :JSON.stringify({ username : $("input[name='username']").val(), password: $("input[name='password']").val(), role : { id : "", name: $("select[name='name']").val() } }), contentType: "application/json;charset=UTF-8", /*如果不写这个,仔细看后台会出现Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported */ dataType:"json", success :function (res) { console.log(res); if(res==100){ $("#msg").html(" success ").show(700).delay(3000).hide(500); }else { $("#msg").html(" fail ").show(700).delay(3000).hide(500); } window.location.href="http://localhost:8080/user/findall?page=1" }, error:function () { $("#msg").html(" fail ").show(700).delay(3000).hide(500); window.location.href="http://localhost:8080/user/findall?page=1" } }); }
后台用@RequestBody接收, @RequestBody只接收JSON对象的字符串
@ResponseBody@RequestMapping(value = "/insert",method = RequestMethod.POST)public String insert(@RequestBody User user){ int result=this.userSerivce.insert(user); if(result==0){ return "101"; } return "100";}