自己实现ajax封装示例分享
代码如下:
//javascript Object: ajax Object //Created By RexLee function Ajax(url,data){ this.url=url; this.data=data; this.browser=(function(){ if(navigator.userAgent.indexOf("MSIE")>0) { return "MSIE";//IE浏览器 }else{ return "other";//其他 }})(); }; Ajax.prototype={ get:function(){ var result; var xmlhttp; if(this.browser=='MSIE'){ try{ xmlhttp=new ActiveXObject('microsoft.xmlhttp'); }catch(e){ xmlhttp=new ActiveXObject('msxml2.xmlhttp'); } }else{ xmlhttp=new XMLHttpRequest(); }; xmlhttp.onreadystatechange=function(){ result = xmlhttp.responseText;//闭包,不能采用this.属性 }; xmlhttp.open('GET',this.url+'?'+this.data,false);//true无法抓取数据,why? xmlhttp.send(null); return result; }, post:function(){ var result; var xmlhttp; if(this.browser=='MSIE'){ xmlhttp=new ActiveXObject('microsoft.xmlhttp'); }else{ xmlhttp=new XMLHttpRequest(); }; xmlhttp.onreadystatechange=function(){ result = xmlhttp.responseText;//闭包,不能采用this.属性 }; xmlhttp.open('POST',this.url,false);//需设为false,否则无法抓取responseText xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");//POST中,这句必须 xmlhttp.send(this.data); return result; } }; //var a=new ajax('opp2.js',''); //alert('by GET\\n'+a.get()) //alert('by POST\\n'+a.post()); /////////////////////////////// window.onload=function(){ document.getElementById("btn").onclick=function(){ var p=document.getElementById("t").value; var a=new Ajax("phpOOP/getPage.php","page="+p); document.getElementById("box").innerHTML=a.get(); }; }
相关推荐
坚持着执着 2020-07-16
坚持着执着 2020-06-14
kentrl 2020-11-10
结束数据方法的参数,该如何定义?-- 集合为自定义实体类中的结合属性,有几个实体类,改变下标就行了。<input id="add" type="button" value="新增visitor&quo
ajaxyan 2020-11-09
zndy0 2020-11-03
学留痕 2020-09-20
Richardxx 2020-11-09
learningever 2020-09-19
chongxiaocheng 2020-08-16
ajaxhe 2020-08-16
lyqdanang 2020-08-16
curiousL 2020-08-03
TONIYH 2020-07-22
时光如瑾雨微凉 2020-07-19
83510998 2020-07-18
jiaguoquan00 2020-07-07
李永毅 2020-07-05