Create ajax

12/28/2009来源:ASP技巧人气:3340

//第一种
var xmlhttp = document.all ? new ActiveXObject("Microsoft.xmlhttp") : new XMLHttPRequest();
var Ajax={
"xmlhttp":xmlhttp,
"get":function(url,execute){
  xmlhttp.open("GET", url, true);
  xmlhttp.onreadystatechange = function()
  {
   if(xmlhttp.readyState == 4 && xmlhttp.status==200)
   {
    execute();
      }
  };
  xmlhttp.send(null);
},
"post":function(){}
};

function invoke(){
var docheckuser=function(){
     var val = xmlhttp.responseText;
     alert(xmlhttp.responseText);
};
Ajax.get("checkUser.do?uname=zhangsan",docheckuser);
}

//第二种
var xmlReq = document.all ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();

var pre="";
function invoke(){
   var url = arguments[0];
   pre=arguments[1];
   for(var n=0;n<arguments.length;n++){

      if(n>1){
       var paramval=document.getElementById(arguments[n]).value;
       if(n==2){
         url=url+"?"+arguments[2]+"="+paramval;
       }else{
         url=url+"&"+arguments[n]+"="+paramval;
       }
      }
   }
   xmlReq.open("GET",url,true);
   xmlReq.onreadystatechange=getResult;
   xmlReq.send(null);
   }
   function getResult(){
   if(xmlReq.readyState==4 && xmlReq.status==200){
      document.getElementById(pre).innerHTML="<font color='red'>"+xmlReq.responseText+"</font>";
   }
}

//第三种
var xmlReq;
var objCheck={
  createXmlReq:function(){
     var len = arguments.length;
     for(var i=0;i<len;i++){
         try{
            xmlReq=eval(arguments[i]());
            break;
         }catch(e){
            continue;
         }
     }
     return xmlReq;
  }
}

function getXmlReq(){
    return objCheck.createXmlReq(
   function(){
      return xmlReq=new ActiveXObject("Microsoft.XMLHTTP");
   },
   function(){
      return xmlReq=new XMLHttpRequest();
   }
   );
}
var pre="";
function invoke(){
   var url = arguments[0];
   pre=arguments[1];
   for(var n=0;n<arguments.length;n++){

      if(n>1){
       var paramval=document.getElementById(arguments[n]).value;
       if(n==2){
         url=url+"?"+arguments[2]+"="+paramval;
       }else{
         url=url+"&"+arguments[n]+"="+paramval;
       }
      }
   }
   xmlReq=getXmlReq();
   xmlReq.open("GET",url,true);
   xmlReq.onreadystatechange=getResult;
   xmlReq.send(null);
   }
   function getResult(){
   if(xmlReq.readyState==4 && xmlReq.status==200){
      document.getElementById(pre).innerHTML="<font color='red'>"+xmlReq.responseText+"</font>";
   }
  }



本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/winterdai/archive/2009/12/28/5089347.aspx