6, jquery Ajax and Java through the post mode interaction

Source: Internet
Author: User

1. Jquery.post(URL, [data], [callback], [type]): Use post to make asynchronous requests

1.1 ParametersURL(String): The URL address of the sending request.Data(MAP): (optional) data to be sent to the server, expressed as a Key/value key-value pair.Callback(function): (optional) The callback function when loading succeeds (the method is called only if the return state of response is success).type(String): (optional) The official description is: Type of data to be sent. The type that should actually be requested for the client (Json,xml, etc.)1.2 jquery Ajax interacts with Java via post1.2.1 HTML code
<body><div id= "main" ><button id= "mybut" >ajax get Data </button><div id= "container" ></ Div></div></body>
1.2.2 Style Code
<style type= "Text/css" > #main {margin:0 auto;width:400px;} #container {width:400px;height:300px;border:1px dashed #666; text-align:center;line-height:300px;} </style>

1.2.3 Javsscript Code
<script type= "Text/javascript" src= "js/jquery-1.8.3.js" ></script><script type= "Text/javascript" >$ (function () {$ (' #myBut '). Click (function () {$.post ("Jqueryajaxservlet", {age:18,name: "Zhang"}, function (data, Textstatus) {var container = $ (' #container '); var resultdata = $.parsejson (data); var = resultdata.age;var name = result Data.name;container.html ("Name:" + name + "," + "Age:" + Age);});}); </script>
1.2.4 Jqueryajaxservlet.java Code
public class Jqueryajaxservlet extends HttpServlet {public void doget (HttpServletRequest request, HttpServletResponse Response) throws Servletexception, IOException {String age = request.getparameter ("Age"); String name = Request.getparameter ("name"); String Personjson = "{\" name "+" \ ": \" "+ name +" \ "," + "\" age "+" \ ":" + Age + "}"; System.out.println (Personjson); Response.getwriter (). write (Personjson);} public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { Doget (request, Response);}}
1.2.5 Web. XML Code
<servlet>    <servlet-name>JqueryAjaxServlet</servlet-name>    <servlet-class> com.jquery.ajax.com.jqueryajaxservlet</servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>JqueryAjaxServlet</servlet-name>    <url-pattern>/jqueryajaxservlet</ url-pattern>  </servlet-mapping>  <welcome-file-list>    <welcome-file>index.jsp </welcome-file>  </welcome-file-list>

6, jquery Ajax and Java through the post mode interaction

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.