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.