Heim  >  Artikel  >  Web-Frontend  >  Jquery Ajax学习实例7 Ajax所有过程事件分析示例_jquery

Jquery Ajax学习实例7 Ajax所有过程事件分析示例_jquery

WBOY
WBOYOriginal
2016-05-16 18:31:431025Durchsuche

一、Ajax所有过程事件分析

   JQuery在执行Ajax的过程中会触发很多事件。
   这些事件可以分为两种事件,一种是局部事件(Local),一种是全局事件(Global)。
   局部事件:可以通过$.ajax来调用,你某一个Ajax请求不希望产生全局的事件,则可以设置global:false。
   全局事件:跟click等事件类似,可以绑定到到每一个DOM元素上。
   这些事件的按照事件的触发顺序如下介绍:

 

局部事件(Local) 全局事件(Global)
ajaxStart 全局事件
开始新的Ajax请求,并且此时没有其他ajax请求正在进行。
beforeSend 局部事件
当一个Ajax请求开始时触发。如果需要,你可以在这里设置XHR对象。
ajaxSend 全局事件
请求开始前触发的全局事件。
success 局部事件
请求成功时触发。即服务器没有返回错误,返回的数据也没有错误。
ajaxSuccess 全局事件
全局的请求成功。
error 局部事件
仅当发生错误时触发。你无法同时执行success和error两个回调函数。
ajaxError 全局事件
全局的发生错误时触发。
complete 局部事件
不管你请求成功还是失败,即便是同步请求,你都能在请求完成时触发这个事件。
ajaxComplete 全局事件
全局的请求完成时触发。
ajaxStop 全局事件
当没有Ajax正在进行中的时候,触发。
注:除了ajaxStart和ajaxStop之外,其他的事件都有3个参数
event, XMLHttpRequest, ajaxOptions
第一个是事件,第二个是XHR对象,第三个参数最有用,是当时调用这个ajax的时候的参数。
对于ajaxError,还有第四个参数thrownError,只有当异常发生时才会被传递。

 

二、Ajax所有过程事件示例

2.1、HTML代码

     

            
           

     

       

Result

      
Process

2.2、Jquery Ajax脚本 

局部事件(Local)实例 全局事件(Global)实例

  

 

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

{
                    $("item", data).each(function(i, domEle) {
                        $("#Result").append("

" + $(domEle).children("title").text() + "
");
                    });
                });
                $("#Process").ajaxStart(function() {
                    alert($(this).text());
                    $(this).text("开始新的Ajax请求");
                });
                $("#Process").ajaxStop(function() {
                    $(this).text("当没有Ajax正在进行中的时候");
                    alert($(this).text());
                });
                $("#Process").ajaxSend(function() {
                    $(this).text("请求开始前");
                    alert($(this).text());
                });
                $("#Process").ajaxSuccess(function() {
                    $(this).text("请求成功");
                    alert($(this).text());
                });
                $("#Process").ajaxComplete(function() {
                    $(this).text("请求完成时");
                    alert($(this).text());
                });
                $("#Process").ajaxError(function() {
                    $(this).text("请求错误时");
                    alert($(this).text());
                });
            });
       
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn