The XMLHttpRequest object is divided into four parts:
1. Create an XMLHttpRequest component
2. Set the callback function
3. initialize XMLHttpRequest Construction
4. Send a request
Instance code:
Copy codeThe Code is as follows:
Var userName;
Var passWord;
Var xmlHttpRequest;
// XmlHttpRequest object
Function createXmlHttpRequest (){
If (window. ActiveXObject) {// if it is an IE browser
Return new ActiveXObject ("Microsoft. XMLHTTP ");
} Else if (window. XMLHttpRequest) {// non-IE browser
Return new XMLHttpRequest ();
}
}
Function onLogin (){
UserName = document. f1.username. value;
PassWord = document. f1.password. value;
Var url = "LoginServlet? Username = "+ userName +" & password = "+ passWord + "";
// 1. Create an XMLHttpRequest component
XmlHttpRequest = createXmlHttpRequest ();
// 2. Set the callback function
XmlHttpRequest. onreadystatechange = zswFun;
// 3. initialize XMLHttpRequest
XmlHttpRequest. open ("POST", url, true );
// 4. Send the request
XmlHttpRequest. send (null );
}
// Callback function
Function zswFun (){
If (xmlHttpRequest. readyState = 4 & xmlHttpRequest. status = 200 ){
Var B = xmlHttpRequest. responseText;
If (B = "true "){
Alert ("Logon successful! ");
} Else {
Alert ("Logon Failed! ");
}
}
}