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("创建xmlhttprequest对象失败");
} 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 method, 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 it is a POST method, it is not empty
5. Process for different response states in the callback function
if(xmlHttp.readyState == 4){ //Determine whether the interaction is successful
if(xmlHttp.status == 200){ //Get the data returned by the server //Get plain text data
var responseText =xmlHttp.responseText;
document.getElementById("info").innerHTML = responseText;
}
}