$ (". Btn"). Click (function () {
Submit data to the server by submitting data in an AJAX way
var sdata = $ ("form"). Serialize ();//serialization of the data on the form
var url = "Reg.do?" +sdata;
$.ajax ({
Type: "GET",
Url:url,
Success:function (data) {
$ ("#info Tr:not (: First)"). Remove ();
for (var i in data) {//i represents an index
var str = "<tr><td>" +data[i].username+ "</td><td>" +data[i].userpwd+ "</td></tr> ";
$ ("#info"). Append (str);
}
},
DataType: "JSON"//setting returns the data format as a JSON object
});
});
The corresponding server-side servlet processing (due to the need to add JSON serialization, so need to introduce the corresponding jar package), the corresponding code:
Req.setcharacterencoding ("UTF-8");
Resp.setcharacterencoding ("Utf-8");//Processing of the return string is garbled
Resp.setcontenttype ("TEXT/HTML,CHARSET=GBK");
String userName = Req.getparameter ("txtUserName");//user name
String userpwd = Req.getparameter ("txtuserpwd");//user password
String useremail = Req.getparameter ("Txtuseremail");//user Email
String Userreason = Req.getparameter ("Txtuserreason");//Registration reason
String useraddress = Req.getparameter ("txtuseraddress");//Residential Address
UserInfo u = new UserInfo ();
U.setusername (UserName);
U.setuserpwd (USERPWD);
U.setregreason (Userreason);
U.setuseremail (UserEmail);
U.setuseraddress (useraddress);
Userinfoservice userservice = new Userinfoservice ();
try {
if (Userservice.userreg (U)) {//Registration succeeded,
list<userinfo> list = Userservice.getlist ();
Serialized operations
Jsonarray Jsonarray = jsonarray.fromobject (list);
String strinfo = Jsonarray.tostring ();//Convert the collection object to a JSON string
System.out.println (Strinfo);
PrintWriter pw = Resp.getwriter ();
Pw.print (Strinfo);
}
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Ajax Applications in jquery