最新写的AJAX类,应该很简单


应用示例:


   格式:


req(


URL,


{


asyn:同步或异步,true|false,默认true即异步


dat:附带的数据,默认为空


rdt:返回数据类型,默认返回文本,可选XML或者JSON


timeout:[毫秒,超时执行的函数],数组,默认[9000,""]


cache:是否缓存,其实就是在URL加随机数


form:是否提交表单,表单的id,如果ID可用那么把Form也提交了。


loading:[‘显示Loading状态ID','显示内容'],数据或者ID,只有ID时显示的文字为正在发送。。。


done:完成时执行的函数,可以是ID或者是函数,为ID时把结果赋给这个ID,只能一个参数


extra:附加的参数就是在原型就是done(a,b,c,d)done的第一个参数是强行加的,就是TXT或XML或JSON,后面的,后面的b,c,d是附加的参数用[b,c,d]数组形式单个时可以不用数组。


})


1.get方式提交,无返回 req("test.php");


2.Post方式提交,无返回 req("test.php",{type:'post'})


3.req('test.php',{asyn:false,type:'post',done:alert,loading:'status',form:'myform',timeout:[1000,'alert("请求时超时")'],cache:false,dat:"a=xx&b=yy&c=zz,rdt:'JSON'"})


以POST方式向test.php提交myform表单,并禁止AJAX缓存,并附带参数a,b,c,提交超过1秒提示超时,在提交过程中在status这个控件中显示‘正在发送请求。。’,完成后alert出来结果,结果是JSON类型会显示为Object


问题:timeout的第二个参数,不能直接是函数,不然刚执行就给执行了。


如果你们有什么建议用QQ跟我联系


function req(u,o){


var P={


   asyn:true,/*Asynchronism*/


   type:"post",


   dat:"",/*Data*/


   rdt:"txt",/*ReponseDataType*/


   timeout:[90000,""],


   cache:true,


   fail:function(e,u,t,d,s){


     u="http://"+window.location.host+getRoot(u);


     alert("错误类型:["+e+"]\nURI:["+u+"]["+t+"]\n\nData:{"+d+"}");


     window.clipboardData.setData("Text",(("post"==t.toLowerCase())?u+(u.match(/\?/)?'&':'?')+d:u))


    }


};


for(var key in o){P[key]=o[key]}


P.type=P.type?P.type.toLowerCase():"get";


var r=getXHR(),L=P.loading,l,S,I,C,X=false,Z;


var _g=function(a){var a=a?a:"AutoGenElement_"+Math.ceil(Math.random()*1000);var s=(typeof(a)=="object")?a:document.getElementById(a);var g=(!s)?true:false;if(g){var j=document.createElement(b?b:"span");j.style.display="inline-block";document.body.appendChild(j)}return (!s)?j:s};


var _s=function(a,b){if(!a)return;try{var o=_g(b);if("|input|textarea|option|".indexOf(o.nodeName.toLowerCase())>-1)o.value=a;else{o.style.display='';o.innerHTML=a}return o}catch(e){}}; var _b=function(s){return(typeof(s)=="string"?(s=document.getElementById(s))?s:false:false)};


var _n=function(s){return(typeof(s)=='undefined'||s=="null"||s==""||s==null?"":escape(s).replace(/\+/g,'%2B').replace(/\"/g,'%22').replace(/\'/g,'%27').replace(/\//g,'%2F'))};


var _t=function(s){return Object.prototype.toString.call(o)==='[object Array]'};


if(!L)l=false;


else if('string'==typeof(L)){l=true;C=null;I=L;S=false}


else if(_t(L)){l=true;I=L[0],C=L[1];S=L[2]}


else l=false;


function _a(u,s){


   if(!u.nodeName)return;


   var v="|"+u.nodeName.toLowerCase()+"|";


   if("|input|textarea|option|".indexOf(v)>-1)


    u.value=s;


   else u.innerHTML=s


}


function _q(s){


   try{


    var x=P.extra;


    var d=P.rdt.toLowerCase();


    var m=d&&d=="xml"?r.responseXML:r.responseText;


    if(d=="json")eval("m="+m);


    if(typeof(s)=="function"){


     var y=[m];


     if(isNull(x))return s(m);


     else if(_t(x))y.push.apply(y,x);


     else y.push(x);


     s.apply(s,y)


    }


    else{


     var t=_b(s);


     if(t)


      _a(t,m);


     else if("string"==typeof(s))


      eval(s);


     else s(m)


    }


   }catch(e){}


}


var _f=function(z){


   var u="",y="",t=z.elements,v,a;


   for(var x=0;x<t.length;++x){


    v=t[x];


    if(!==""){


     a=undefined;


     switch(v.type){


      case"select-one":


       if(v.selectedIndex>-1)


        a=v.options[v.selectedIndex].value;


       else a="";


       break;


      case"select-multiple":


       var s=v.options;


       for(var w=0;w<s.length;++w){


        if(s[w].selected)


         u+=y++"="+_n(s[w].value);


        y="&"


       }


       break;


      case"checkbox":


      case"radio":


       if(v.checked)a=v.value;


       break;


      default:


       a=v.value;


       break


     }


     if(a!=undefined){


      a=_n(a);


      u+=y++"="+a;y="&"


     }


    }


   }


return u


};


if(_b(P.form)){


   var F=P.form;


   F=F?_b(F):null;


   if(!F||F.nodeName!="FORM")return;


   K=F.getAttribute("method");


   P.type=(!K)?"get":K.toLowerCase();


   var c=_f(F);


   if(c.length===0)return;


   P.dat+=P.dat.length>0?'&'+c:c


}


var P_=(P.type=="post")?true:false;


var U=u+=(P.cache)?"":((u.match(/\?/)?'&':'?')+"ascrnd="+new Date().getTime());


if(!Post)U+=(U.match(/\?/)?'&':'?')+P.dat;


var Z=setTimeout(function(){X=true;r.abort()},P.timeout[0]);


var _r=function(){


   if(X){


    if(l)show("发送请求超时",I)


    eval(P.timeout[1])


   }


   else if(r.readyState==4){


    clearTimeout(Z);


    try{


     if(r.status==200) {


      if(l){


       show("请求已经完成",I);


       if(S&&!isNaN(S))setTimeout(function(){(I=_b(I))?I.style.display='none':''},S)


      }


      _q(P.done)


     }


     else{


      var T="";


      switch(r.status){


       case 400:T="HTTP 错误 400 - 请求格式错误";break;


       case 403:T="HTTP 错误 403 - 禁止访问:访问被拒绝";break;


       case 404:T="HTTP 错误 404 - 文件或目录未找到";break;


       case 500:T="HTTP 错误 500 - 服务器内部错误";break;


       default:T="请求发生错误!\n"+r.status+"/"+r.statusText


      }


      if(P.fail){


       if(l)show(T,I);


       P.fail(T,U,P.type,P.dat,r.status)


      }


     }


    }catch(e){}


   }


};


try{


   r.open(P.type,U,P.asyn)


   if(l)_s(C?C:"正在发送...",I);


   if(P_)r.setRequestHeader("Content-Type","application/x-www-form-urlencoded");


   r.send((P.type=="get")?null:P.dat);


   if(P.asyn)r.onreadystatechange=_r;else _r()


}catch(e){if(l)(_s(e.message,I))}


};