【经典】AJAX详解

 

加载请求: .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()取代。

转载至:https://www.cnblogs.com/by-dxm/p/6393288.html

猜你喜欢

转载自blog.csdn.net/GeeLoong/article/details/81663014