3, in the Ajax in the Java interaction process, add loading pictures

Source: Internet
Author: User

1, in the Ajax in the Java interaction process, add loading pictures

1.1 HTML code

<body><button id= "MYBTN" > Click </button><div id= "Myajax" ></div><!--analog wait picture--></body>

1.2 JavaScript code

<script type= "Text/javascript" >//1, gets to Xmlhttprequestfunction getxmlhttp () {var xmlhttp;if (window.  XMLHttpRequest) {//code for ie7+, Firefox, Chrome, Opera, safarixmlhttp = new XMLHttpRequest ();} else {//code for IE6, Ie5xmlhttp = new ActiveXObject ("Microsoft.XMLHTTP");} return XMLHTTP;} Window.onload = function () {document.getElementById (' mybtn '). onclick = function () {//6.1 When you click Get Ajax request, set the waiting buffer picture, Change it to show var myimg = document.getElementById ("myimg"); myimg.style.display = "block";//1, get to XMLHttpRequest object var xmlhttp = Getxmlhttp ();//2, XMLHttpRequest response Event Xmlhttp.onreadystatechange = function () {if (xmlhttp.readystate = = 4 && Xmlhttp.status = = 200) {//6.2 when clicked to get Ajax request successfully, set the waiting buffer picture, change it to hide Myimg.style.display = "none";//5, the information obtained by the operation/ 5.1ajax of data returned, converted to JavaScript object//5.1.1 using Eval to achieve conversion var Ajaxresult = eval ("(" + Xmlhttp.responsetext + ")");//5.1.2 Using the JQuery method, for Jquery.parsejson (), the key value must be double quotes//var ajaxresult = Jquery.parsejson (Xmlhttp.responsetext); */alert (Ajaxresult)//5.2 get JavaScript objectsThe attribute var age = Ajaxresult.age;var name = ajaxresult.name;//5.3 Gets the div to the page and sets the content var mydiv = document.getElementById ("Myajax" mydiv.innerhtml = "Name:" + name + "," + "Age:" + Age;}} 3, ready to get Ajax request//3.1 for GET request, with parameter way, directly in the Open function request address with parameter/* Xmlhttp.open ("GET", "Ajaxserverlet?age=18&name=zhang" , true); */xmlhttp.open ("POST", "Ajaxserverlet", true),//4, send Ajax request//4.1 for POST request mode, transfer parameters inside the Send method, the transmitted parameters must be string// 4.2 for POST request submission parameters, to set the request header for the form submission, mimic the form's post submission Xmlhttp.setrequestheader ("Content-type", "application/ X-www-form-urlencoded "); Xmlhttp.send (" Age=18&name=zhang ");};} </script>

1.3 Style Code

<style type= "TEXT/CSS" >/* analog network bad waiting picture */  #myAjax {position:relative;width:400px;height:400px;border:1px Solid red;text-align:center;font-size:16;line-height:400px;font-weight:bold;} #myimg {position:absolute;left:50%;top:50%;margin-left: -150px;margin-top: -150px;display:none;} </style>

1.4 Ajaxserverlet.java Code

public class Ajaxserverlet extends HttpServlet {public void doget (HttpServletRequest request, HttpServletResponse Response) throws Servletexception, IOException {String age = request.getparameter ("Age"); String name = Request.getparameter ("name"); person person = new Person (name, age);//analog Network delay try {thread.sleep (n);} catch (Interruptedexception e) {E.printstacktrac E ();} 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.5 Web. XML code

<servlet><servlet-name>AjaxServerlet</servlet-name><servlet-class> Com.ajax.com.ajaxserverlet</servlet-class></servlet><servlet-mapping><servlet-name> Ajaxserverlet</servlet-name><url-pattern>/ajaxserverlet</url-pattern></servlet-mapping ><welcome-file-list><welcome-file>index.html</welcome-file></welcome-file-list>

3, in the Ajax in the Java interaction process, add loading pictures

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.