The jquery serialization form form uses AJAX to post processing the returned JSON data

Source: Internet
Author: User

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

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.