简单ajax类, 比较小, 只用ajax功能时, 可以考虑它

忘了哪儿转来的了, 不时的能够用上, 留存一下

<script language="javascript" type="text/javascript"> /*** var ajax=Ajax(); /*get使用方式* / ajax.get("php_server.php?id=1&name=xxx", function(data){ alert(data); //data为从服务器端读取的数据 }); /*第一种post使用方式* / ajax.post("php_server.php", "name=xxx&age=23", function(data){ alert(data); }); /*第二种post使用方式* / ajax.post("php_server.php", {name:"kkkkkxxxx",age:"34"},function(data){ alert(data); }); */ </script> //---------------------- function Ajax(recvType){ var aj=new Object(); aj.recvType=recvType ? recvType.toUpperCase() : 'HTML'; //向形参中传递的文件类型 aj.targetUrl=''; aj.sendString=''; aj.resultHandle=null; /*创建XMLHttpRequest对象*/ aj.createXMLHttpRequest=function(){ var xmlHttp = false; if(window.XMLHttpRequest){ //在非IE中创建XMLHttpRequest对象 xmlHttp = new XMLHttpRequest(); }else if(window.ActiveXObject){ try{ xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); //按新版IE创建 }catch(error1){ //创建失败 try{ xmlHttp = new ActiveXobject("Microsoft.XMLHttp"); //按老版IE创建 }catch(error2){ //创建失败 xmlHttp = false; } } } return xmlHttp; } aj.XMLHttpRequest=aj.createXMLHttpRequest(); /*处理服务器的响应*/ aj.processHandle=function(){ if(aj.XMLHttpRequest.readyState == 4){ if(aj.XMLHttpRequest.status == 200){ if(aj.recvType=="HTML") aj.resultHandle(aj.XMLHttpRequest.responseText); else if(aj.recvType=="XML") aj.resultHandle(aj.XMLHttpRequest.responseXML); } } } /*定义使用get方法传递的方法*/ aj.get=function(targetUrl, resultHandle){ aj.targetUrl=targetUrl; if(resultHandle!=null){ aj.XMLHttpRequest.onreadystatechange=aj.processHandle; aj.resultHandle=resultHandle; } if(window.XMLHttpRequest){ aj.XMLHttpRequest.open("get", aj.targetUrl); aj.XMLHttpRequest.send(null); }else{ aj.XMLHttpRequest.open("get", aj.targetUrl, true); aj.XMLHttpRequest.send(); } } /*定义使用post方法传递的方法*/ aj.post=function(targetUrl, sendString, resultHandle){ aj.targetUrl=targetUrl; if(typeof(sendString)=="object"){ var str=""; for(var pro in sendString){ str+=pro+"="+sendString[pro]+"&"; } aj.sendString=str.substr(0, str.length-1); }else{ aj.sendString=sendString; } if(resultHandle!=null){ aj.XMLHttpRequest.onreadystatechange=aj.processHandle; aj.resultHandle=resultHandle; } aj.XMLHttpRequest.open("post", targetUrl); aj.XMLHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); aj.XMLHttpRequest.send(aj.sendString); } return aj; } --------压缩代码---- eval(function(p,a,c,k,e,d){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--){d[e(c)]=k[c]||e(c)}k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return p}('7 I(a){i b=h H();b.m=a?a.G():"t";b.6="";b.j="";b.5=8;b.q=7(){i c=r;3(l.2){c=h 2()}9{3(l.z){y{c=h z("J.K")}s(e){y{c=h M("F.L")}s(d){c=r}}}}u c};b.2=b.q();b.k=7(){3(b.2.N==4){3(b.2.E==A){3(b.m=="t"){b.5(b.2.D)}9{3(b.m=="C"){b.5(b.2.B)}}}}};b.p=7(d,c){b.6=d;3(c!=8){b.2.v=b.k;b.5=c}3(l.2){b.2.o("p",b.6);b.2.n(8)}9{b.2.o("p",b.6,S);b.2.n()}};b.w=7(g,d,c){b.6=g;3(W(d)=="Z"){i f="";10(i e V d){f+=e+"="+d[e]+"&"}b.j=f.P(0,f.T-1)}9{b.j=d}3(c!=8){b.2.v=b.k;b.5=c}b.2.o("w",g);b.2.U("11-X","O/x-Q-R-Y");b.2.n(b.j)};u b};',62,64,'||XMLHttpRequest|if||resultHandle|targetUrl|function|null|else||||||||new|var|sendString|processHandle|window|recvType|send|open|get|createXMLHttpRequest|false|catch|HTML|return|onreadystatechange|post||try|ActiveXObject|200|responseXML|XML|responseText|status|Microsoft|toUpperCase|Object|Ajax|Msxml2|XMLHTTP|XMLHttp|ActiveXobject|readyState|application|substr|www|form|true|length|setRequestHeader|in|typeof|Type|urlencoded|object|for|Content'.split('|'),0,{})) ----------------

 

posted @ 2016-12-05 10:51  青松梧桐  阅读(160)  评论(0编辑  收藏  举报