Requirement: Spring MVC receives the JSON data submitted by Ajax and deserializes it into an object with the following code:
Front JS Code:
//property corresponds to the object property with the conversionvarParam={name: ' Chinese ', price:16};$.ajax ({URL:"/book/adddata", type:"POST", DataType:' JSON ', //required setting, background @requestbody will do the data deserialization according to itContentType: "Application/json", //The JSON data must be submitted in a string formatdata:JSON.stringify (param), success:function(data) {alert (' Add success '); }, Error:function(XMLHttpRequest, textstatus) {alert (' Add failed '); }});
Background Java code:
@RequestMapping (value= "AddData") @ResponseBody public Protocol adddata (Model Model, @RequestBody book ) {= bookservice.add (book); return redata; }
Spring MVC receives the JSON data submitted by Ajax and deserializes it into an object