AJAX实现方法,XMLHttpRequest对象的方法

AJAX实现步骤,XMLHttpRequest对象的方法

① AJAX实现步骤


AJAX实现方法,XMLHttpRequest对象的方法

② XMLHttpRequest 对象


AJAX实现方法,XMLHttpRequest对象的方法

③ XMLHttpRequest对象的属性


AJAX实现方法,XMLHttpRequest对象的方法

④ XMLHttpRequest对象的方法


AJAX实现方法,XMLHttpRequest对象的方法

⑤ 使用AJAX发送请求及处理响应


AJAX实现方法,XMLHttpRequest对象的方法

⑥ GET请求和POST请求的区别


AJAX实现方法,XMLHttpRequest对象的方法

⑦ 文本和XML方式响应的区别


AJAX实现方法,XMLHttpRequest对象的方法

//创建XMLHttpRequestc对象
var  xmlHttp=false;

function createXMLHttpRequest(){
  if(window.ActiveXObject){//IE浏览器
    try{
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){
      try{
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }catch(e){}
    }
  }else if(window.XMLHttpRequest) {//其他浏览器:如mozilla 的 fireFox 或者 netscape 7
    xmlHttp=new XMLHttpRequest();
    if(xmlHttp.overrideMimeType) {
      xmlHttp.overrideMimeType("text/html");
    }
  }
}

function processResponse(){
  if(xmlHttp.readyState==4){
    if(xmlHttp.status==200){
      var info=xmlHttp.responseText;
//      var info=xmlHttp.responseXML;
    }else{
      alert("你所请求的页面有异常。");
    }
  }else {
    //div.innerText="sending data..."
  }
}

function sendRequest(url){
  createXMLHttpRequest();
  xmlHttp.open("POST",url,true);
  xmlHttp.onreadystatechange=processResponse;
//  xmlHttp.send(null);
  xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded;charset=UTF-8');
  xmlHttp.send("username=XXXXX&password=XXXXX&other=XXXX");
}