The only discussion here is to transfer the JSON data remotely through Ajax, without discussing passing the data as a form
The first way:
The data passed by the front end is exactly the same as our Bean entity object properties, and can be accepted in the form of an object.
Front-end notation:
1 $ (). Ready (function(){2 varobj = json.stringify ({' Userno ': ' 121589 ', ' processId ': ' A ', ' processName ': ' Test approval ', ' description ': ' This is a test '});3 alert (obj);4 5 //background uses objects as accepted6 $.ajax ({7URL: "Testjsonone",8Type: "POST",9 Data:obj,TenContentType: ' Application/json;charset=utf-8 ', OneSuccess:function(data) { AAlert ("Haha"); - } - }); the -});
Particular points to note:
1. contentType: ' Application/json;charset=utf-8 ', specifies that the data is passed in JSON form
2. To pass the data, if a JSON-formatted string, it is best to first create a JSON object, and then use Json.stringify () to convert to a string form
Background:
// Object Form accepts front-end data @RequestMapping (value= "/testjsonone", method = requestmethod.post) @ResponseBody public String Testjson (@RequestBody Pomessagevo Pomessagevo) { System.out.println (pomessagevo.tostring ()); return "Success"; }
Note the point:
1. Front-end data and bean object properties are consistent
2. @requestbody annotations are used, and this annotation is imported into the jar package first, Jackson-core-asl-xxx.jar and Jackson-mapper-asl-xxx.jar two packages
3. Note Usage do not know, Baidu a bit
The second way:
It is possible for the front end to pass over the data many very complex, then we can use the form of string in the back end to accept, the specific processing, we in the back end ourselves processing.
Front-end notation:
// The background uses the form of a string to accept the $.ajax ({ "Testjsontwo", "POST", //data: {jsonstr: ' Nihao '}, data: {jsonstr:obj}, //data:obj, // ContentType: ' Application/json;charset=utf-8 ', function(data) { alert (" Haha "); });
Note the comparison with the above:
1. JSONSTR Specifies that the background can only be accepted by this name
Do not specify ContentType on Ajax : ' Application/json;charset=utf-8 '
Background:
// accepts the front-end data @RequestMapping as a string (value= "/testjson", method = requestmethod.post) @ResponseBody public string Testjson (String jsonstr) { System.out.println (jsonstr); // Here you can use Jsonmapper to handle JSONSTR, such as turning into the desired bean object return "Success";}
Front-end Ajax delivers JSON data, back-end processing two ways