Instance:
1. Ajax login advantages: If the entered password is incorrect, the page will be refreshed.
<Script language = "JavaScript">
// Define the XMLHTTPRequest object instance
VaR http_request = false;
// Define reusable HTTP request sending Functions
Function send_request (method, URL, content, responsetype, callback) {// initialize, specify the processing function, and send the request Function
Http_request = false;
// Start initializing the XMLHTTPRequest object
If (window. XMLHttpRequest) {// Mozilla Browser
Http_request = new XMLHttpRequest ();
If (http_request.overridemimetype) {// sets the mime category
Http_request.overridemimetype ("text/XML ");
}
}
Else if (window. activexobject) {// IE browser
Try {
Http_request = new activexobject ("msxml2.xmlhttp ");
} Catch (e ){
Try {
Http_request = new activexobject ("Microsoft. XMLHTTP ");
} Catch (e ){}
}
}
If (! Http_request) {// exception. An error occurred while creating the object instance.
Window. Alert ("the XMLHTTPRequest object instance cannot be created .");
Return false;
}
If (responsetype. tolowercase () = "text "){
// Http_request.onreadystatechange = processtextresponse;
Http_request.onreadystatechange = callback;
}
Else if (responsetype. tolowercase () = "XML "){
// Http_request.onreadystatechange = processxmlresponse;
Http_request.onreadystatechange = callback;
}
Else {
Window. Alert ("response type parameter error. ");
Return false;
}
// Determine the request sending method and URL and whether to asynchronously execute the next code
If (method. tolowercase () = "get "){
Http_request.open (method, URL, true );
Http_request.setrequestheader ("If-modified-since", "0 ");
}
Else if (method. tolowercase () = "Post "){
Http_request.open (method, URL, true );
Http_request.setrequestheader ("Content-Type", "application/X-WWW-form-urlencoded ");
}
Else {
Window. Alert ("the HTTP request type parameter is incorrect. ");
Return false;
}
Http_request.send (content );
}
// Function for processing the returned text format information
Function processtextresponse (){
If (http_request.readystate = 4) {// judge the object status
If (http_request.status = 200) {// The information has been returned successfully. Start to process the information.
// Alert (http_request.responsetext );
Alert ("Text Document response. ");
} Else {// The page is abnormal.
Alert ("the page you requested has an exception. ");
}
}
}
Function registercheck (){
Document. getelementbyid ("feedback_info"). innerhtml = "";
VaR F = Document. Forms [0];
If (F. useraccount. value! = "" & F. Password. value! = ""){
Document. getelementbyid ("feedback_info"). innerhtml = "processing the request. Please wait ...";
Send_request ("Post ","? Service = page/register & useraccount = "+ F. useraccount. Value +" & Password = "+ F. Password. Value, null," text ", showfeedbackinfo );
}
Else {
Document. getelementbyid ("feedback_info"). innerhtml = "enter the correct user name and password. ";
}
}
Function showfeedbackinfo (){
If (http_request.readystate = 4) {// judge the object status
// Alert (http_request.responsetext );
If (http_request.status = 200) {// The information has been returned successfully. Start to process the information.
// Alert (http_request.responsetext );
If (http_request.responsetext = 'OK '){
Document. Forms [0]. Submit ();
}
Else {
Document. getelementbyid ("feedback_info"). innerhtml = http_request.responsetext;
}
} Else {// The page is abnormal.
Alert ("the page you requested has an exception. ");
}
}
}
</SCRIPT>
2. Ajax implements timed refresh. (To be continued)