Example of post method usage in jquery

  • 2020-03-30 04:11:44
  • OfStack

This article illustrates the use of the post method in jquery for your reference. Specific usage analysis is as follows:

Implement site in using jquery asynchronous interactions, the two functions which are commonly used for the get and post methods, use the get method is very simple, can be directly submit a get request, if you have parameters, just behind the appended to the url directly, but when using the post method, pass parameters, need from url to write, makes the participation in trouble, but it safer to do it, and the code is the probability of lower in Chinese (the get method in many cases will appear garbled phenomenon in Chinese), here is a detailed introduction of post is how to pass parameters.

First write an HTML code as follows:

<html>
<head>
  <title>jquery post Methods 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>
</head>
<body>
   <input name="name" id="name"/>
   <input name="pass" id="pass"/>
   <input type="button" value=" test " onclick="testPost();"/>
</body>
</html>

 
The code for the post method in the server-side servlet is as follows (the servlet class is named login and its access path is configured as servlet/login)
  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 running the above HTML code on the client side, click the "test" button to bring up   Name: entered username pass: entered password dialog box, through the analysis of the HTML code, you can know that the post method passed parameters in the json format of the data.

Supplement:

In the case of garbled Chinese characters, The default Chinese character encoding for ajax is utf-8 , the encoding of the received post page should remain the same.


Related articles: