Sample Code for asynchronous transmission and verification implemented by Ajax

Source: Internet
Author: User

Ajax asynchronous transmission is widely used. Let's take web development as an example. When a user registers, how can he determine whether the user exists immediately and display a prompt after the input box? This uses asynchronous transmission ~~

It allows you to retrieve the data you want in the background without leaving the page and display it on the current page !! Okay. Let's take a look at the example below.

The following is an Ajax verification code.
Copy codeThe Code is as follows:
<Script type = "text/javascript">

// ① Set the variable
Var xmlHttp;
Var flag = false;
Var package_name;

// ② Create an XMLHttp object
Function createXMLHttp (){
If (window. XMLHttpRequest ){
XmlHttp = new XMLHttpRequest ();


} Else {
XmlHttp = new ActiveXObject ("Microsoft. XMLHTTP ");

}

}

// ③
Function getPackage_name (package_name ){

// Create an XMLHttpRequest object
CreateXMLHttp ();

Document. getElementById ("msg"). innerHTML = package_name;
// Create an HTTP request and send package_name to AppInfoServlet in post Mode
XmlHttp. open ("post", "AppInfoOneServlet? Package_name = "+ package_name );


// Call the callback function to generate a visual page response

XmlHttp. onreadystatechange = checkCallback;

// Send the request
XmlHttp. send (null );

// Friendly prompt
Document. getElementById ("msg"). innerHTML = "getting information. Please wait ...";

}

// ④ Callback function
Function checkCallback (){

If (xmlHttp. readyState = 4 ){
If (xmlHttp. status = 200 ){

Var text = xmlHttp. responseText;
Var t = text. split ('|'); // when multiple values are obtained and separated with "|"
Document. getElementById ("msg "). innerHTML = t [0] + "<br>" + "<div id = 'Container'>" + "<div id = 'content'>" + "<div id = 'slider'> "+" <ul> "+" <li> "+" "+" </li> "+" <li> "+" "+" </li> "+" </ul> "+" </div> "+" // The obtained data is displayed in the id "msg", for example, <span id = "msg"> </span>
}
}
}
</Script>

In this way, js functions are called on the jsp page.
Copy codeThe Code is as follows:
<A href = "# bottom" onclick = "getPackage_name ('<% = ainfo. getPackage_name () %>')"> Get </a>

The data obtained by Ajax is displayed in the following tag:
Copy codeThe Code is as follows:
<Span id = "msg"> </span>

Okay. Let's take a look at the servlet code:
Copy codeThe Code is as follows:
Public void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {

Request. setCharacterEncoding ("UTF-8 ");
Response. setCharacterEncoding ("UTF-8"); // It must be before the out; otherwise, the transmitted man is garbled.
PrintWriter out = response. getWriter ();


String package_name = request. getParameter ("package_name ");

AppDaoImpl adi = new AppDaoImpl ();
AppInfo ainfo = new AppInfo ();
Ainfo = adi. getOneAppInfo (package_name );


Out. print (ainfo. getDescription () + "|" + ainfo. getscreen_export path () + "|" + ainfo. getScreen_2_path () + "|" + ainfo. getDownload_path ());
}

The above is clear. Of course, it is the same as verifying whether a user exists. Pay attention to the garbled Characters During Chinese Character transmission ~~~~~~

Related Article

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.