ajax封装的data数据,只要key跟pojo的属性一致,就会自动封装到对应的pojo。
<script type="text/javascript">
$.ajax({
type: "post",
/*
contentType:"application/json charset=utf-8",报错
Invalid mime type "application/json charset=UTF-8":
Invalid token character ' ' in token "json charset=UTF-8"
*/
contentType:"application/json;charset=utf-8",
url: "helloJson.action",
data: '{"username":"admin","sex":"man"}',
success: function(data){
alert("派工成功");
}
});
</script>
@RequestMapping("helloJson")
public void helloJson(@RequestBody User user) throws Exception{
System.out.println("前端的数据为"+user);
}
结果:
前端的数据为User [id=null, username=admin, sex=man, birthday=null, address=null]
我们也可以接受整个请求的json数据
public Object insertOrUpdateSalorderAndSalorderg(@RequestBody String param) ,这样 param就是请求 json。