jquery serialization Form form uses AJAX to submit after processing the returned JSON data _jquery

Source: Internet
Author: User
Tags eval serialization

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.