/**
* 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);
},
}
jQuery ajax方法提炼
最新推荐文章于 2021-08-06 08:37:40 发布