Code for sending a request through XMLHttpRequest in JavaScript
Source: Internet
Author: User
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! ");
}
}
}
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.