The Post method usage instance in jquery _jquery

Source: Internet
Author: User

This example describes the use of the Post method in jquery and shares it for your reference. The specific usage analysis is as follows:

When using jquery to implement asynchronous interactions in a Web site, the two functions commonly used are get and post methods, the Get method is simple to submit a GET request, if there are parameters, directly appended to the URL after the line, but when using the Post method, pass the parameter, need to write separately with the URL, Make the reference become troublesome, but this is more safe to do, and the probability of the occurrence of Chinese garbled is also lower (get method in many cases will be garbled phenomenon), here is a detailed description of how the post is passed parameters.

First write an HTML code as follows:

Copy Code code as follows:
<title>jquery Post method Test </title>
<script language= "javascript" src= "Jquery.min.js" ></script>
<script type= "Text/javascript" >
function Testpost () {
var name=$ ("#name");
var pass=$ ("#pass");
$.post ("Servlet/login", {NAME:NAME,PASS:PASS},POSTCB);
}
function POSTCB (date) {
alert (date);
}
</script>
<body>
<input name= "name" id= "name"/>
<input name= "Pass" id= "pass"/>
<input type= "button" value= "Test" onclick= "Testpost ();" />
</body>


The Post method code in the server-side servlet is as follows (Servlet class named login, configuring its access path to Servlet/login)
Copy Code code as follows:
Response.setcontenttype ("text/html");
PrintWriter out = Response.getwriter ();
Request.setcharacterencoding ("GB18030");
String name= request.getparameter ("name");
String pass= request.getparameter ("Pass");
Out.print ("Name:" +name+ "pass:" +pass);
Out.flush ();
Out.close ();

After the client runs the above HTML code, clicking the "Test" button pops up the name: Enter the password dialog box, and by parsing the HTML code you know that the Post method pass parameter uses the data JSON format.

Add:

for the occurrence of Chinese characters garbled, Ajax default encoding is Utf-8, the post receive page encoding should also maintain the same encoding.

The

wants this article to help you with the Ajax-based design of your 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.