js-重写jquery的ajax中的内容

 

 

/**
 * 测试
 * 2018/1/8 13:40 lee.wangel
 */
(function($){
    //备份jquery的ajax方法
    var _ajax=$.ajax;
    //重写jquery的ajax方法
    $.ajax=function(opt){
        //备份opt中error和success方法
        var fn = {
            error:function(XMLHttpRequest, textStatus, errorThrown){},
            success:function(data, textStatus){}
        }
        if(opt.error){
            fn.error=opt.error;
        }
        if(opt.success){
            fn.success=opt.success;
        }
        //扩展增强处理
        var _opt = $.extend(opt,{
            error:function(XMLHttpRequest, textStatus, errorThrown){
                //错误方法增强处理
                fn.error(XMLHttpRequest, textStatus, errorThrown);
            },
            success:function(data, textStatus,xhr){
                //console.log( '-------->,ajax 被 代理');
                //成功回调方法增强处理
                if(data.code =='-2'){
                    setTimeout(function () {
                        var topPage = getTopWinow();
                        topPage.location.href ='/';
                    },1000)
                    return;
                };
                fn.success(data, textStatus);
            }
        });
        return _ajax(_opt); // 用于返回,防止第三方插件绑定了事件在ajax上
    };
})(jQuery);

顶级页面

/**
 * 在页面中任何嵌套层次的窗口中获取顶层窗口
 * @return 当前页面的顶层窗口对象
 */
function getTopWinow(){
    var p = window;
    while(p != p.parent){
        p = p.parent;
     }
    return p;
}

 

posted @ 2018-01-08 19:57  243573295  阅读(1153)  评论(1编辑  收藏  举报