亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Jquery Ajax學(xué)習(xí)實(shí)例7 Ajax所有過(guò)程事件分析示例

 更新時(shí)間:2010年03月23日 00:13:13   作者:  
JQuery在執(zhí)行Ajax的過(guò)程中會(huì)觸發(fā)很多事件。

一、Ajax所有過(guò)程事件分析

   JQuery在執(zhí)行Ajax的過(guò)程中會(huì)觸發(fā)很多事件。
   這些事件可以分為兩種事件,一種是局部事件(Local),一種是全局事件(Global)。
   局部事件:可以通過(guò)$.ajax來(lái)調(diào)用,你某一個(gè)Ajax請(qǐng)求不希望產(chǎn)生全局的事件,則可以設(shè)置global:false。
   全局事件:跟click等事件類似,可以綁定到到每一個(gè)DOM元素上。
   這些事件的按照事件的觸發(fā)順序如下介紹:

 

局部事件(Local) 全局事件(Global)
ajaxStart 全局事件
開始新的Ajax請(qǐng)求,并且此時(shí)沒有其他ajax請(qǐng)求正在進(jìn)行。
beforeSend 局部事件
當(dāng)一個(gè)Ajax請(qǐng)求開始時(shí)觸發(fā)。如果需要,你可以在這里設(shè)置XHR對(duì)象。
ajaxSend 全局事件
請(qǐng)求開始前觸發(fā)的全局事件。
success 局部事件
請(qǐng)求成功時(shí)觸發(fā)。即服務(wù)器沒有返回錯(cuò)誤,返回的數(shù)據(jù)也沒有錯(cuò)誤。
ajaxSuccess 全局事件
全局的請(qǐng)求成功。
error 局部事件
僅當(dāng)發(fā)生錯(cuò)誤時(shí)觸發(fā)。你無(wú)法同時(shí)執(zhí)行success和error兩個(gè)回調(diào)函數(shù)。
ajaxError 全局事件
全局的發(fā)生錯(cuò)誤時(shí)觸發(fā)。
complete 局部事件
不管你請(qǐng)求成功還是失敗,即便是同步請(qǐng)求,你都能在請(qǐng)求完成時(shí)觸發(fā)這個(gè)事件。
ajaxComplete 全局事件
全局的請(qǐng)求完成時(shí)觸發(fā)。
ajaxStop 全局事件
當(dāng)沒有Ajax正在進(jìn)行中的時(shí)候,觸發(fā)。
注:除了ajaxStart和ajaxStop之外,其他的事件都有3個(gè)參數(shù)
event, XMLHttpRequest, ajaxOptions
第一個(gè)是事件,第二個(gè)是XHR對(duì)象,第三個(gè)參數(shù)最有用,是當(dāng)時(shí)調(diào)用這個(gè)ajax的時(shí)候的參數(shù)。
對(duì)于ajaxError,還有第四個(gè)參數(shù)thrownError,只有當(dāng)異常發(fā)生時(shí)才會(huì)被傳遞。

 

二、Ajax所有過(guò)程事件示例

2.1、HTML代碼

      <div>

            <input type="button" onclick="BtnSpareClick();" value="PartEvents" />
            <input type="button" onclick="BtnGlobalClick();" value="GlobalEvents" />

      </div>

       <div id="Result">Result</div>
       <div id="Process">Process</div>

2.2、Jquery Ajax腳本 

局部事件(Local)實(shí)例 全局事件(Global)實(shí)例

  <script language="javascript" type="text/javascript">
            $.ready(function BtnSpareClick() {
                $.ajax({
                    type: "get",
                    url: "http://chabaoo.cn/windy2008/rss",
                    data: {},
                    global: false,
                    beforeSend: function(data, status, settings) {
                        $("#Process").text("Part請(qǐng)求開始前");
                        alert($("#Process").text());
                    },
                    success: function(data, status, settings) {
                        $("item", data).each(function(i, domEle) {
                            $("#Result").append("<div>" + $(domEle).children("title").text() + "</div>");
                        });
                        $("#Process").text("Part請(qǐng)求成功時(shí)");
                        alert($("#Process").text());
                    },
                    complete: function(data, status, settings) {
                        $("#Process").text("Part請(qǐng)求完成時(shí)");
                        alert($("#Process").text());
                    },
                    error: function(data, status, settings) {
                        $("#Process").text("Part請(qǐng)求錯(cuò)誤時(shí)");
                        alert($("#Process").text());
                    }
                });
            });

</script>

 <script language="javascript" type="text/javascript">

 $.ready(function BtnGlobalClick() {
                $.get("http://chabaoo.cn/windy2008/rss", {}, function(data, status, settings)

{
                    $("item", data).each(function(i, domEle) {
                        $("#Result").append("<div>" + $(domEle).children("title").text() + "</div>");
                    });
                });
                $("#Process").ajaxStart(function() {
                    alert($(this).text());
                    $(this).text("開始新的Ajax請(qǐng)求");
                });
                $("#Process").ajaxStop(function() {
                    $(this).text("當(dāng)沒有Ajax正在進(jìn)行中的時(shí)候");
                    alert($(this).text());
                });
                $("#Process").ajaxSend(function() {
                    $(this).text("請(qǐng)求開始前");
                    alert($(this).text());
                });
                $("#Process").ajaxSuccess(function() {
                    $(this).text("請(qǐng)求成功");
                    alert($(this).text());
                });
                $("#Process").ajaxComplete(function() {
                    $(this).text("請(qǐng)求完成時(shí)");
                    alert($(this).text());
                });
                $("#Process").ajaxError(function() {
                    $(this).text("請(qǐng)求錯(cuò)誤時(shí)");
                    alert($(this).text());
                });
            });
        </script>

相關(guān)文章

最新評(píng)論