This article mainly introduces jQuery serializing form form form, using Ajax to submit and process the returned JSON data. Please refer to the following for your friends
1. Return JSON string: the code is 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. Convert the returned JSON string to JSON object through Eval: the code is 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){ $("#b Indshipmmsidiv "). Show(); VAR TRS =" "; for (VaR I = 0; I < ships. Length; I + +) {TRS + =" tr > < td > "+ ships [i]. MMSI +" < / td > < td > "+ ships [i]. Veselname +" < td > < tr > ";} $(" bindshipmmsidiv table tbody "). Append (TRS); / / register 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); $("#bindSh Ipmmsidiv "). Hide();});}}}}); 3. Through jQuery's $(" form "). Serialize(), the data of the form form can be serialized and submitted to the background, so the form form can be operated and the returned data can be processed through Ajax. Myform input [name= Godowdown warrior. Code] "). Val();} else {alert (res.message);}}}); 4. How to prevent garbled Code: JSP page: charset: UTF-8 servlet: UTF-8 filter: UTF-8 add a response.setcharacterencoding (" UTF-8 ") before printwriter out = response. Getwriter(). But remember to put it before you declare printwwrite. In a word, the front-end interface, java files, database and database connection all adopt unified coding, so as to avoid disorderly code and other situations
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.