First, return in a Modelandview way
First look at the JavaScript code:
1 /**2 * Save-Sync (version control Library)3 */4 functionSavesynchronizedvchorse (obj) {5 varSS = $ ("#SynchronizedSelection div");6 varCacheselectary =NewArray ()7 for(vari = 0; i < ss.length; i + +) {8 //Alert (Ss.eq (i). html ());9 //Alert (Ss.eq (i). Find (' label '). html ());Ten varO=ss.eq (i). Find (' label '). Find ("input[type= ' checkbox ']"); One A vars = $ (o). attr (' checked ');//because of $ (obj). attr ("checked", false); Checked property is - if(s! =NULL) { -Cacheselectary.push ($ (O). attr (' value '))); the } - } - if(Cacheselectary.length = = 0) { -Alert ("Please select City"); + return; - } + varJSON = "synchronizedbean={\" exceptionid\ ": \" "+exceptionid+" \ ", \" cityids\ ": [" A for(vari = 0; i < cacheselectary.length; i + +) { at if(i = = Cacheselectary.length-1) { -JSON = json + "\" "+ cacheselectary[i" + "\" "; -}Else { -JSON = json + "\" "+ cacheselectary[i] +" \ ","; - } - } inJSON = JSON + "]}"; - $.ajax ({ toType: "POST", +DataType: "JSON", -URL: ".. /main/savesynchronizeddata ", the Data:json, *Successfunction(msg) { $ alert (msg.main);Panax Notoginseng }, -Errorfunction() {//XMLHttpRequest, Textstatus, Errorthrown theAlert ("Request Failed"); + } A }); the}
Mainly look at the AJAX request section.
Then look at the code for the controller in spring:
1@RequestMapping (value = "/savesynchronizeddata", method =requestmethod.post)2 Public@ResponseBody Modelandview Savesynchronizeddata (@RequestParam ("Synchronizedbean") String Msynchronizedjson) {3Objectmapper Objectmapper =NewObjectmapper ();4map<string, string> map =NewHashmap<string, string>();5 Try {6Synchronizedbean bean = Objectmapper.readvalue (Msynchronizedjson, Synchronizedbean.class);7 if(Bean! =NULL) {8string[] ary =bean.getcityids ();9 if(ary! =NULL&& ary.length > 0) {Ten for(String s:ary) { OneVchousepo PO =NewVchousepo (); A Po.setexceptionid (Bean.getexceptionid ()); - Po.setcustomercode (s); -Po.setcreatetime (NewDate ()); thePo.setexceptionstate (0); - Vchservice.add (PO); - } - } + } -Map.put ("MSG", "Success"); +}Catch(jsonparseexception e) { A e.printstacktrace (); atMap.put ("MSG", "Error"); -}Catch(jsonmappingexception e) { - e.printstacktrace (); -Map.put ("MSG", "Error"); -}Catch(IOException e) { - e.printstacktrace (); inMap.put ("MSG", "Error"); - } to return NewModelandview (NewMappingjackson2jsonview (), map); +}
Here is the way to return JSON by Modelandview. In addition: The package used by Mappingjackson2jsonview is
import Org.springframework.web.servlet.view.json.MappingJackson2JsonView;
Second, the form of the return string
Do not experiment this way, please see http://hw1287789687.iteye.com/blog/2128304
Spring MVC in AJAX requests and returns JSON