• 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()取代。

  • 相关阅读:
    python虚拟环境--virtualenv
    求导法则和高阶导数
    restful规范
    python协程--yield和yield from
    打印质数
    更改docker的存储位置
    同台ECS安装多个mysql
    jenkins + gitlab + harbor + k8s 部署项目
    jenkins 配置maven
    gitlab + jenkins 持续集成
  • 原文地址:https://www.cnblogs.com/by-dxm/p/6393288.html
Copyright © 2020-2023  润新知