1. Return the JSON string:
/** Output A string to the browser*/ protected voidWritejson (String json) {PrintWriter pw=NULL; Try{Servletresponse.setcontenttype ("Text/plain;charset=utf-8"); PW=Servletresponse.getwriter (); Pw.write (JSON); Pw.flush (); Pw.close (); } Catch(IOException e) {e.printstacktrace (); } finally { if(PW! =NULL) {pw.close (); } } }
2. Convert the returned JSON string to a JSON object by using eval:
$.ajax ({data:{"Shipmmsi": Shipmmsi,"ShipName": shipname}, URL:"shipbk/findshipmmsiandname.do", Async:true, type:"POST", Success:function (data) {varShips = eval ('('+ Data +')'); $("#bindShipmmsiDiv Table Tbody"). HTML (""); if(ships!=NULL){ if(ships.length) {$ ("#bindShipmmsiDiv"). Show (); vartrs=""; for(varI=0; i<ships.length;i++) {TRS+="<tr><td>"+ships[i].mmsi+"</td><td>"+ships[i].vesselname+"</td></tr>"; } $("#bindShipmmsiDiv Table Tbody"). Append (TRS); //register a Click event for TR$("#bindShipmmsiDiv table Tbody TR"). Click (function () {$ ( This). AddClass ('select_tr'). Siblings (). Removeclass ('select_tr'); }); $("#bindShipmmsiDiv table Tbody TR"). DblClick (function () {Fillshipmmsiandname ( This); $("#bindShipmmsiDiv"). Hide (); }); } } } });
3. $ ("form") through jquery. Serialize () can serialize the form's data into the background, so you can manipulate the form form and process the returned data through Ajax.
$.ajax ({URL:'deliverywarrant/update.do', Data: $ ('#myform'). Serialize (), type:"POST", Success:function (data) {varres = eval ('('+ Data +')'); if(res && res.success = =true) {alert (res.message); Location.href="/godownwarrant/findtodeliverywarrant.do?godownwarrant.code="+$("#myform Input[name=godownwarrant\\.code]"). Val (); } Else{alert (res.message); }});
4, to prevent garbled processing methods:
JSP page: Charset:utf-8
Servlet:utf-8
Filter:utf-8
Add a sentence before printwriter out = Response.getwriter ()
Response.setcharacterencoding ("UTF-8") can solve the problem of garbled.
But keep in mind that you must put it before the declaration printwwrite.
In short, the foreground interface, Java files, database and database connection are unified code, will not appear garbled, etc.
The jquery serialization form form uses AJAX to post processing the returned JSON data