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

① AJAX实现步骤




② XMLHttpRequest 对象




③ XMLHttpRequest对象的属性




④ XMLHttpRequest对象的方法




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




⑥ GET请求和POST请求的区别




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




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

 

猜你喜欢

转载自4636.iteye.com/blog/2328765