Ajax创建

Ajax创建

//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");

你可能感兴趣的:(Ajax创建)