1. Returns the JSON string:
Copy Code code as follows:
/** output A string to the browser * *
protected void Writejson (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. Converts the returned JSON string to a JSON object via eval:
Copy Code code as follows:
$.ajax ({
data:{
"Shipmmsi": Shipmmsi,
"ShipName": ShipName
},
URL: "Shipbk/findshipmmsiandname.do",
Async:true,
Type: "POST",
Success:function (data) {
var ships = eval (' + data + ') ');
$ ("#bindShipmmsiDiv table Tbody"). HTML ("");
if (ships!=null) {
if (ships.length) {
$ ("#bindShipmmsiDiv"). Show ();
var trs= "";
for (Var i=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, through jquery $ ("form"). Serialize () Can serialize data from form forms to the background, so you can manipulate form forms and process the returned data through Ajax.
Copy Code code as follows:
$.ajax ({
URL: ' Deliverywarrant/update.do ',
Data: $ (' #myform '). Serialize (),
Type: "POST",
Success:function (data) {
var res = 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 remember to be sure to put it before the declaration printwwrite.
In short, the front interface, Java files, database and database connections are unified code, will not appear garbled and so on
PS: About JSON operation, here we recommend a few more practical JSON online tools for your reference to use:
Online JSON code inspection, inspection, landscaping, formatting tools:
Http://tools.jb51.net/code/json
JSON Online formatting tool:
Http://tools.jb51.net/code/jsonformat
Online Xml/json Mutual Conversion tool:
Http://tools.jb51.net/code/xmljson
JSON code online Format/beautify/compress/edit/Convert tools:
Http://tools.jb51.net/code/jsoncodeformat
Online JSON compression/escape tool:
Http://tools.jb51.net/code/json_yasuo_trans
C Language Style/html/css/json code formatting landscaping Tools:
Http://tools.jb51.net/code/ccode_html_css_json