Ajax Applications in jquery

Source: Internet
Author: User

$ (". 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

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.