js實現對ajax請求物件導向的封裝_javascript技巧

來源:互聯網
上載者:User

AJAX 是一種用於建立快速動態網頁的技術。通過在後台與伺服器進行少量資料交換,AJAX 可以使網頁實現非同步更新。這意味著可以在不重新載入整個網頁的情況下,對網頁的某部分進行更新。
在js中使用ajax請求一般包含三個步驟:

  •               1、建立XMLHttp對象
  •               2、發送請求:包括開啟連結、發送請求
  •               3、處理響應

在不使用任何的js架構的情況下,要想使用ajax,可能需要向下面一樣進行代碼的編寫

<span style="font-size:14px;">var xmlHttp = xmlHttpCreate();//建立對象 xmlHttp.onreadystatechange = function(){//響應處理   if(xmlHttp.readyState == 4){     console.info("response finish");     if(xmlHttp.status == 200){        console.info("reponse success");       console.info(xmlHttp.responseText);     }   } } xmlHttp.open("get","TestServlet",true);//開啟連結  xmlHttp.send(null);//發送請求  function xmlHttpCreate() {   var xmlHttp;   try {     xmlHttp = new XMLHttpRequest;// ff opera   } catch (e) {     try {       xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");// ie     } catch (e) {       try {         xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");       } catch (e) {        }     }   }   return xmlHttp; }  console.info(xmlHttpCreate());</span> 

如果在比較複雜的商務邏輯裡面使用這種ajax請求,會使得代碼很臃腫,不方便重用,並且可以看到,可能在伺服器響應成功後要處理一個商務邏輯操作,這個時候不得不把操作寫在onreadystatechage方法裡面。
為了方便代碼的重用我們可以做出如下處理;  

  •       1、伺服器響應成功後,要處理的商務邏輯交給開發人員自己處理 
  •       2、對請求進行物件導向的封裝  

處理之後看起來應該像下面這個樣子: 

<pre code_snippet_id="342814" snippet_file_name="blog_20140513_2_2489549" name="code" class="javascript">window.onload = function() {   document.getElementById("hit").onclick = function() {     console.info("開始請求");     ajax.post({         data : 'a=n',         url : 'TestServlet',         success : function(reponseText) {           console.info("success : "+reponseText);         },         error : function(reponseText) {           console.info("error : "+reponseText);         }     });   } }  var ajax = {   xmlHttp : '',   url:'',   data:'',   xmlHttpCreate : function() {     var xmlHttp;     try {       xmlHttp = new XMLHttpRequest;// ff opera     } catch (e) {       try {         xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");// ie       } catch (e) {         try {           xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");         } catch (e) {          }       }     }     return xmlHttp;   },   post:function(jsonObj){     ajax.data = jsonObj.data;     ajax.url = jsonObj.url;     //建立XMLHttp對象,開啟連結、請求、響應     ajax.xmlHttp = ajax.xmlHttpCreate();     ajax.xmlHttp.open("post",ajax.url,true);     ajax.xmlHttp.onreadystatechange = function(){       if(ajax.xmlHttp.readyState == 4){         if(ajax.xmlHttp.status == 200){           jsonObj.success(ajax.xmlHttp.responseText);         }else{           jsonObj.error(ajax.xmlHttp.responseText);         }       }     }     ajax.xmlHttp.send(ajax.data);   } };

上述代碼實現了類似jquery中的ajax操作,希望對大家的學習有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.