日期:2014-05-16  浏览次数:20584 次

简单ajax封装类
function ajax(getorpost,url,data,success,failure){
   var xmlHttpRequest;
   if(window.ActiveXObject){
       xmlHttpRequest=new ActiveXObject("Microsoft.XMLHTTP");
   }else{
       xmlHttpRequest = new XMLHttpRequest();
   }

   var statechange = function(){
      if(xmlHttpRequest.readyState==4){
         if(xmlHttpRequest.status==200){
            success(xmlHttpRequest.responseText);
         }else if(xmlHttpRequest.status==404){
            failure(xmlHttpRequest.responseText);
         }
      }
   }
   xmlHttpRequest.onreadystatechange = statechange;
   if(getorpost=="post"){
     xmlHttpRequest.open(getorpost,url,true);
     xmlHttpRequest.setRequestHeader("content-length",data.length);
     xmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
     xmlHttpRequest.send(data);
   }else if(getorpost=="get"){
     xmlHttpRequest.open(getorpost,url,true);
     xmlHttpRequest.send(null);
   }