zoukankan      html  css  js  c++  java
  • 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()取代。

  • 相关阅读:
    安卓表格布局android:collapseColumns,android:shrinkColumns和stretchColumn
    Cocos2dx 学习记录 [2] 关于混合和高亮一些知识点的体会
    01背包问题
    textarea文本域宽度和高度(width、height)自己主动适应变化处理
    SSL协议具体解释
    Geeks Union-Find Algorithm Union By Rank and Path Compression 图环算法
    Linux内核源代码分析方法
    linux服务之svn
    java实现第七届蓝桥杯冰雹数
    java实现第七届蓝桥杯冰雹数
  • 原文地址:https://www.cnblogs.com/by-dxm/p/6393288.html
Copyright © 2011-2022 走看看