Robust AJAX source code learning application example

Source: Internet
Author: User

This example mainly demonstrates how to operate XMLHttpRequest .....
Introduction to XMLHttpRequest
To realize such a brilliant miracle, you must be very familiar with a JavaScript Object, XMLHttpRequest. This small object has actually existed in several browsers for a while. It is the core of Web 2.0, Ajax, and most other content to be introduced in the next few months of this column. To help you quickly understand the object in a large scale, the following describes a few methods and attributes that will be used for this object.
Open (): Create a new request to the server.
Send (): send a request to the server.
Abort (): exit the current request.
ReadyState: Provides the ready status of the current HTML.
ResponseText: Request Response text returned by the server.
Client HTML code:
Copy codeThe Code is as follows:
<Script language = "javascript" type = "text/javascript">
Var xmlHttp = false;
Try {
XmlHttp = new XMLHttpRequest ();
} Catch (trymicrosoft ){
Try {
XmlHttp = new ActiveXObject ("Msxml2.XMLHTTP ");
} Catch (othermicrosoft ){
Try {
XmlHttp = new ActiveXObject ("Microsoft. XMLHTTP ");
} Catch (failed ){
XmlHttp = false;
}
}
}
If (! XmlHttp)
Alert ("Error initializing XMLHttpRequest! ");
Function getCustomerInfo (){
Var phone = document. getElementById ("qq"). value;
Var url = "demo2.asp? Qq = "+ escape (phone );
XmlHttp. open ("GET", url, true );
XmlHttp. onreadystatechange = updatePage;
XmlHttp. send (null );
}
Function updatePage (){
If (xmlHttp. readyState = 4 ){
If (xmlHttp. status = 200 ){
Var response = xmlHttp. responseText. split ("| ");
Document. getElementById ("message "). innerHTML = 'Number: '+ response [0] +' <br> name: '+ response [1] +' <br> gender: '+ response [2] +' <br> title: '+ response [3];
Alert ("response service completed! ");
}
Else if (xmlHttp. status = 404 ){
Alert ('requested URL does not exist! ');
}
Else {
Alert ('error: Error code: '+ xmlHttp. status );
}
}
}
</Script>
<Input id = "qq" type = "text" onchange = "getCustomerInfo ()"/>
<Div id = "message"> Please enter my QQ number: 178010108. The returned details are displayed. </div>

Server program code:
Copy codeThe Code is as follows:
<%
Response. ContentType = "text/xml"
Response. CharSet = "GB2312"
If request ("qq") = "178010108" then
Response. write "178010108 | alimama | male | ASP technology"
Else
Response. write "This QQ number is an empty number"
End if
%>

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.