5, jquery Ajax and Java through the get mode of interaction

Source: Internet
Author: User

1, Jquery.get (URL, [data], [callback]): Use Get method to make asynchronous request

1.1 Parameters: URL(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, will be appended to the request URL as jquerystring. Callback(function): (optional) The callback function when loading succeeds (the method is called only if the return state of response is success).The callback function can be called when the request succeeds. If you need to execute the function on error, use $.ajax.
1.2 jquery Ajax and Java through get-mode interaction1.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 JavaScript code
<script type= "Text/javascript" src= "js/jquery-1.8.3.js" ></script><script type= "Text/javascript" >$ (function () {$ (' #myBut '). Click (function () {$.get ("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>

5, jquery Ajax and Java through the get mode of 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.