jQuery ajax方法提炼

/**
 * AJAX
 * 2016-12-30
 * wangbt
 */
var jqueryAjax = {

        /**
         * jQuery Ajax 同步/异步方法
         * @param {Object} url 请求地址 -- 必须
         * @param {Object} type 请求类型 -- 非必须
         * @param {Object} cache 是否缓存此页面  true:缓存; false:不缓存 -- 非必须
         * @param {Object} datas 参数 -- 非必须
         * @param {Object} dataType 返回值类型 -- 非必须
         * @param {Object} async 是否同步 -- 非必须
         * @param {Object} succFun 成功回调函数 -- 非必须
         * @param {Object} errorFun 失败回调函数 -- 非必须
         * @param {Object} beforeSend 请求提交前回调(可用于防止用户频繁提交)-- 非必须
         * @param {Object} complete 请求完后回调(可用于防止用户频繁提交) -- 非必须
         * 【注:返回JSON时,JSON中包含successCode、successMsg、errorCode、errorMsg和object,分别表示成功编码、成功提示信息、失败编码、失败提示信息和内容信息】
         */
        sysAysAjax: function(url, type, cache, datas, dataType, async, succFun, errorFun, beforeSend, complete){
            $.ajax({
                type: (type == null || typeof(type) == "undefined") ? "post" : type,
                url:url,
                cache: (cache == null || typeof(cache)=="undefined") ? true : cache,
                data: (datas == null || typeof(datas)=="undefined") ? {} : datas,
                dataType: (dataType == null || typeof(dataType)=="undefined") ? "TEXT" : dataType,
                async:(async != null && async != undefined) ? async : true,
                scriptCharset: 'utf-8',
                statusCode: {404: function(){
                    sweetAlert.error("提示", "访问地址无效");
                }},
                beforeSend: (beforeSend == null || beforeSend == undefined) ? null : beforeSend,
                complete: function(XMLHttpRequest, textStatus){// 该函数在success之后执行
                    var sessionStatus = XMLHttpRequest.getResponseHeader("sessionStatus");
                    if(sessionStatus == "timeout"){// session超时跳回到登录页面【注:timeout是后台拦截器返回的,具体拦截器怎么写,可以在我博客中找,有一篇是专门写拦截器的】
                        window.location.replace(common.projectPath + "login.jsp");
                    }

                    if(complete != null && complete != undefined){
                        complete();
                    }
                },
                success: function(data){
                    if(data != null) {
                        if(dataType == "TEXT") {// 字符串直接返回
                            succFun(data);
                        } else if(dataType == "JSON") {
                            if(data.errorCode == undefined) {
                                succFun(data);
                            } else if(data.errorCode != 0){
                                if(errorFun != null && errorFun != undefined){
                                    errorFun(data.errorMsg);
                                }else{
                                    sweetAlert.caution("温馨提示", data.errorMsg, "确定", 5000);// 这里使用的是一个提示框插件
                                }
                            }else{
                                if(succFun != null && succFun != undefined){
                                    succFun(data.object);
                                }else{
                                    sweetAlert.success("温馨提示", data.successMsg, "确定", 3000);// 这里使用的是一个提示框插件
                                }
                            }
                        }
                    }
                },
                error: function(data){
                    sweetAlert.error("系统提示", "程序暂停使用,请联系客服!", "关闭", 5000);// 这里使用的是一个提示框插件
                }
            });
        },

        /**
         * jQuery Ajax 异步方法
         * @param {Object} url 请求地址
         * @param {Object} datas 参数
         * @param {Object} succFun 成功回调函数
         * @param {Object} errorFun 失败回调函数
         */
        synchronizeAjax: function(url, datas, succFun, errorFun){
            jqueryAjax.sysAysAjax(url, "POST", null, datas, "JSON", true, succFun, errorFun);
        },

        /**
         * jQuery Ajax 同步方法
         * @param {Object} url 请求地址
         * @param {Object} datas 参数
         * @param {Object} succFun 成功回调函数 
         * @param {Object} errorFun 失败回调函数
         */
        asynchronusAjax: function(url, datas, succFun, errorFun){
            jqueryAjax.sysAysAjax(url, "POST", null, datas, "JSON", false, succFun, errorFun);
        },

        /**
         * 返回TEXT类型的同步AJAX
         * @param {Object} url 请求地址
         * @param {Object} datas 参数
         * @param {Object} fun 回调函数 
         */
        asynchronusAjaxText: function(url, datas, fun) {
            jqueryAjax.sysAysAjax(url, "POST", null, datas, "TEXT", false, fun);
        },

        /**
         * 返回TEXT类型的异步AJAX
         */
        synchronizeAjaxText: function(url, datas, fun) {
            jqueryAjax.sysAysAjax(url, "POST", null, datas, "TEXT", true, fun);
        },
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值