Ajax :六個全域事件

來源:互聯網
上載者:User

標籤:blog   success   button   global   error:   status   ide   form   document   

載入請求: .ajaxStart() 和 .ajaxstop()

    $(document).ajaxStart(function(){        $(‘.loading‘).show();    }).ajaxStop(function(){        $(‘.loading‘).hide();    });

錯誤處理: .ajaxError()

    //1 $.ajax()使用屬性提示錯誤    $(‘form input[type=button]‘).click(function(){        $.ajax({            type:"post",            url:"test1.php",            async:true,            data:$(‘form‘).serialize(),            success:function(response,status,xhr){                $(‘#box‘).html(response);            },//            timeout:3000//            global:false            error:function(xhr,errorText,errorType){//                alert(errorText + ‘:‘ +errorType);                alert(xhr.status + ‘:‘ +xhr.statusText);            }                    });    });        //2 $.post()使用連綴.error()方法提示錯誤,將被.fail()取代    $(‘form input[type=button]‘).click(function(){        $.post(‘test1.php‘).error(function(xhr,errorText,errorType){            alert(errorText + ‘:‘ +errorType);            alert(xhr.status + ‘:‘ +xhr.statusText);        });    });        //3 使用全域.ajaxError()方法    $(document).ajaxError(function(event,xhr,settings,info){        alert(event.type);        alert(event.target);        for(var i in event){ //列印出event的所有屬性            document.write(i + ‘<br />‘);        }    });

.ajaxSuccess(),對應一個局部方法:.success(),請求成功完成時執行。

.ajaxComplete(),對應一個局部方法:.complete(),請求完成後註冊一個回呼函數。

.ajaxSend(),沒有對應的局部方法,只有屬性 beforeSend,請求發送之前要綁定的函數。

    //$.post()使用全域    $(‘form input[type=button]‘).click(function(){        $.post(‘test.php‘,$(‘form‘).serialize());    });        $(‘document‘).ajaxSend(function(){        alert(發送請求之前執行);    }).ajaxComplete(function(response,status,xhr){        alert(‘請求完成後,不管是否失敗成功‘);    }).ajaxSuccess(function(event,xhr,settrings){        alert(‘請求成功後,不管是否成功‘);    }).ajaxError(function(event,xhr,settrings){        alert(‘請求失敗後‘);    });        //$.post()使用局部    $(‘form input[type=button]‘).click(function(){        $.post(‘test.php‘,$(‘form‘).serialize()).success(function(){            alert(‘請求成功後‘);        }).complete(function(){            alert(‘請求完成後‘);        }).error(function(){            alert(‘請求失敗後‘);        });    });        //$.ajax()使用屬性    $(‘form input[type=button]‘).click(function(){        $.ajax({            type:"post",            url:"test1.php",            async:true,            data:$(‘form‘).serialize(),            success:function(response,status,xhr){                alert(‘請求成功後‘);            },            complete:function(){                alert(‘請求完成後,不管失敗成功‘);            },            beforeSend:function(){                alert(‘發送請求之前‘);            },            error:function(xhr,errorText,errorType){                alert(‘請求失敗後‘);            }        });    });    

註:

在 jQuery1.5 版本以後,使用.success()、.error()和.complete()連綴的方法,可以用.done()、.fail()和.always()取代。

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.