To transmit JSON data to the backend at the front end, JSON data must be converted (through the stringify method of json2.js ),
VaR DATA = {name: "XXXX", age: "24 "};
Alert (JSON. stringify (data ))
VaR url = "<% = context %>/calculateaction. do? Method = jsontest & name = Zheng & Data = "+ JSON. stringify (data );
$. Ajax ({
URL: URL,
Datatype: "jsonp ",
Success: function (data ){
Alert (data );
}
});
Processing in the background is relatively simple. You can use JSON objects for processing:
Public modelandview jsontest (httpservletrequest request, httpservletresponse response ){
Try {
Object Data = request. getparameter ("data"); // obtain JSON data
Jsonobject so = jsonobject. fromobject (data); // convert it to a JSON object
System. Out. println (so. Get ("name") + "" + so. Get ("Age"); // get data
Printwriter out = response. getwriter (); // gets the print stream
Out. Print ("XXXX"); // print the returned data.
Out. Close ();
} Catch (exception e ){
E. printstacktrace ();
}
Return NULL;
}