php ajax封装类,自己实现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.οnlοad=function(){

document.getElementById("btn").οnclick=function(){

var p=document.getElementById("t").value;

var a=new Ajax("phpOOP/getPage.php","page="+p);

document.getElementById("box").innerHTML=a.get();

};

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值