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
%>