最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

js實現(xiàn)對ajax請求面向?qū)ο蟮姆庋b

 更新時間:2016年01月08日 15:36:40   作者:andywuchuanlong  
這篇文章主要介紹了js實現(xiàn)對ajax請求面向?qū)ο蟮姆庋b的相關(guān)資料,需要的朋友可以參考下

AJAX 是一種用于創(chuàng)建快速動態(tài)網(wǎng)頁的技術(shù)。通過在后臺與服務(wù)器進(jìn)行少量數(shù)據(jù)交換,AJAX 可以使網(wǎng)頁實現(xiàn)異步更新。這意味著可以在不重新加載整個網(wǎng)頁的情況下,對網(wǎng)頁的某部分進(jìn)行更新。
在js中使用ajax請求一般包含三個步驟:

  •               1、創(chuàng)建XMLHttp對象
  •               2、發(fā)送請求:包括打開鏈接、發(fā)送請求
  •               3、處理響應(yīng)

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

<span style="font-size:14px;">var xmlHttp = xmlHttpCreate();//創(chuàng)建對象 
xmlHttp.onreadystatechange = function(){//響應(yīng)處理 
  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);//發(fā)送請求 
 
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> 

如果在比較復(fù)雜的業(yè)務(wù)邏輯里面使用這種ajax請求,會使得代碼很臃腫,不方便重用,并且可以看到,可能在服務(wù)器響應(yīng)成功后要處理一個業(yè)務(wù)邏輯操作,這個時候不得不把操作寫在onreadystatechage方法里面。
為了方便代碼的重用我們可以做出如下處理;  

  •       1、服務(wù)器響應(yīng)成功后,要處理的業(yè)務(wù)邏輯交給開發(fā)人員自己處理 
  •       2、對請求進(jìn)行面向?qū)ο蟮姆庋b  

處理之后看起來應(yīng)該像下面這個樣子: 

<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; 
    //創(chuàng)建XMLHttp對象,打開鏈接、請求、響應(yīng) 
    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); 
  } 
};

上述代碼實現(xiàn)了類似jquery中的ajax操作,希望對大家的學(xué)習(xí)有所幫助。

相關(guān)文章

最新評論

凉山| 怀仁县| 永泰县| 滦平县| 密山市| 石景山区| 榕江县| 南部县| 睢宁县| 抚州市| 犍为县| 舟曲县| 临高县| 永吉县| 常熟市| 白朗县| 资兴市| 化隆| 资溪县| 波密县| 漳州市| 白城市| 柯坪县| 合川市| 丰县| 卓尼县| 呼伦贝尔市| 枞阳县| 方城县| 阿拉善右旗| 许昌市| 陆川县| 博白县| 蒲城县| 永丰县| 阿克苏市| 大港区| 舟曲县| 密云县| 凉山| 五原县|