//1 創建對象
var xmlHttp;
function createXMLHttpRequest(){
if(window.ActiveXObject)
xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
else if(widow.XMLHttpRequest)
xmlHttp = new XMLHttprequest();
}
//2建立請求
♠
var firstName = document.getElementById("firstName").value;
var url= "9-3.aspx?"+ new Date().getTime();
xmlHttp.open("GET",url+ "firstName=" + firstName ,ture)//ture表示異步 get方法在提交數據時候在queryString 中發送數據
♣
xmlHttp.open("POST",url);//第4步發送數據時候用xmlHttp.send(firstName)
//3異步對象鏈接服務器
xmlHttp.onreadystatechange = function(){
if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
var responseDiv = document.getElementById("serverResponse");//xmlHttp.responseText服務器的返回并賦值
responseDiv.innerHTML = decodeURI(xmlHttp.responseText); //解碼
}
//4數據發送
xmlHttp.send(null)
2步驟當為post時候
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");