1. Create an xmlHttpRequest object
If (window. XMLHttpRequest ){
XmlHttp = new XMLHttpRequest ();
If (xmlHttp. overrideMimeType ){
XmlHttp. overrideMimeType ("text/xml ");
}
} Else if (window. ActiveXobject ){
Var activeName = ["MSXML2.XMLHTTP", "Microsoft. XMLHTTP"];
For (var I = 0; I <activeName. length; I ++ ){
Try {
XmlHttp = new ActiveXobject (activeName [I]);
Break;
} Catch (e ){
}
}
}
If (! XmlHttp ){
Alert ("failed to create xmlhttprequest object ");
} Else {
}
2. Set the callback function
XmlHttp. onreadystatechange = callback;
Function callback (){}
3. Use the OPEN method to establish a connection with the server xmlHttp. open ("get", "ajax? Name = "+ name, true)
In this step, pay attention to setting the http Request Method (post/get). If it is POST, pay attention to setting the request header information xmlHttp. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded ")
4. send data to the server
XmlHttp. send (null );
If the POST method is used, it is not empty.
5. process different response statuses in the callback function
If (xmlHttp. readyState = 4 ){
// Determine whether the interaction is successful
If (xmlHttp. status = 200 ){
// Obtain the data returned by the server
// Obtain plain text data
Var responseText = xmlHttp. responseText;
Document. getElementById ("info"). innerHTML = responseText;
}
}