This article mainly introduces the jquery serialization form form, using AJAX to post a sample of the returned JSON data, and the friends you need can refer to the following
1. Return the JSON string:
* * 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. 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) {
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. $ ("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) {
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 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