首頁  >  文章  >  web前端  >  自己實作ajax封裝範例分享_javascript技巧

自己實作ajax封裝範例分享_javascript技巧

WBOY
WBOY原創
2016-05-16 16:53:531004瀏覽

複製程式碼 程式碼如下:

 //javascript Object: Created By RexLee
function Ajax(url,data){
    this.url=url;
    this.data=data;
   .userAgent.indexOf("MSIE")>0) {  
            return "MSIE";//IE瀏覽器
    
    }})( );
};
Ajax.prototype={
    get:function(){
       .browser=='MSIE') {
            try{
                 catch(e){
                    xmlhttp=new ActiveXObject('msxml2.xmlhttp 🎜>            }else{
                     xmlhttp.onreadystatechange=function(){
            result = xmlhttp.responseText;//閉包,且無法採用this.屬性
        };
        xmlhttp.open('GET',this.url '?' this.data,false);//true 🎜>        return result;
    },
    post:function(){
          if(this.browser=='MSIE'){
            xmlhttp =new ActiveXObject('microsoft.xmlhttp');
            }else{
             };
        xmlhttp.onreadystatechange=function(){
            result = xmlhttp. responseText;//閉包,不能採用this.屬性
        };
        xmlhttp.open('POST',this.url,false);//需要設定為falseText,否則無法抓取response> xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");//POST中,這句話必須
        xmlhttp.send(this.data);
    }
};

//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(){
  .getElementById("t").value;
    var a=new Ajax("phpOOP/getPage.php","page=" p);
    document.getElementById("box").inininHTML=a. get();
    };
}


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn