1.建立xmlHttpRequest對(duì)象
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("創(chuàng)建xmlhttprequest對(duì)象失敗");
} else {}
2.設(shè)置回調(diào)函數(shù)
???xmlHttp.onreadystatechange = callback;
?? function callback(){}
3.使用OPEN方法與服務(wù)器建立連接??xmlHttp.open("get","ajax?name="+ name,true)
此步注意設(shè)置http的請(qǐng)求方式(post/get),如果是POST方式坦报,注意設(shè)置請(qǐng)求頭信息xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
4.向服務(wù)器端發(fā)送數(shù)據(jù)
??xmlHttp.send(null);
? 如果是POST方式就不為空
5.在回調(diào)函數(shù)中針對(duì)不同的響應(yīng)狀態(tài)進(jìn)行處理
??if (xmlHttp.readyState == 4){ ????? //判斷交互是否成功
????? if (xmlHttp.status == 200){ ??????? //獲取服務(wù)器返回的數(shù)據(jù) ??????? //獲取純文本數(shù)據(jù)
??????? ????var responseText =xmlHttp.responseText;
???????????document.getElementById("info").innerHTML = responseText;
????? }
?? }