Asynchronous transfer of AJAX implementations related to validation sample code _ajax

Source: Internet
Author: User
Ajax asynchronous transmission is very broad, take the web development, when the user registration, when the user just lost, how to immediately determine the existence of users, and in the input box after the display prompts? This is the asynchronous transfer ~ ~





it allows you to not leave the page in case you get the data you want in the background and display it on the current page!! OK, let's look at an example





The following is an AJAX validation code


Copy Code code as follows:

<script type= "Text/javascript" >

//① settings variable
var xmlHttp;
var flag = false;
Var package_name; The

//② creates a XMLHTTP object
function Createxmlhttp () {
if window. XMLHttpRequest) {
XmlHttp = new XMLHttpRequest ();


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

}

}

//③
Function Getpackage_name (package_name) {

//Create XMLHttpRequest Object
Createxmlhttp ();

document.getElementById ("msg"). Innerhtml=package_name;
//Create an HTTP request to post the package_name to Appinfoservlet
Xmlhttp.open ("Post", "Appinfooneservlet?package_name=" +package_name);


//Call callback function to generate a visual page response

Xmlhttp.onreadystatechange =checkcallback;

//Send request
Xmlhttp.send (NULL);

//Friendly tip
document.getElementById ("msg"). InnerHTML = "Getting information, please wait patiently ...";

}

//④ callback functionBr>function Checkcallback () {

if (xmlhttp.readystate = 4) {
if (xmlhttp.status = =) {

var tex t = Xmlhttp.responsetext;
var t=text.split (' | '); /When you get more than one value and use the "|"
document.getElementById ("MSG") when split. InnerHTML =t[0]+ "<br>" + "<div id= ' container ' >" + "<div id=" Content ' > ' + "<div id= ' slider ' >" + "<ul>" + "<li>" + " "+" </li> "+" <li> "+" ' + ' </li> ' + ' </ul> "+" </div> "+" //Gets data is displayed in the tag with id "MSG", for example: <span id= "msg" ></span>
}
}
}
</script>
/div>



in the JSP page is the way to call the JS function


Copy Code code as follows:

<a href= "#bottom" onclick= "Getpackage_name (' <%=ainfo.getpackage_name ()%> ')" > Get </a>



Ajax-acquired data will be displayed in the following markup


Copy Code code as follows:

<span id= "MSG" ></span>



OK, let's take a look at the code in the servlet:


Copy Code code as follows:

public void DoPost (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {

Request.setcharacterencoding ("Utf-8");
Response.setcharacterencoding ("Utf-8");/must be out before, otherwise transmission of the man for garbled AH
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_1_path () + "|" +ainfo.getscreen_2_path () + "|" +ainfo.getdownload_path ());
}



above already very clear, of course, verify the existence of the user, the truth and this is exactly the same, which must pay attention to the problem of Chinese characters in the transmission of garbled ~~~~~~
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.