ajax封装
Posted tuhazi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ajax封装相关的知识,希望对你有一定的参考价值。
ajax: {
//返回弹出层信息
showloading:function prg_show_load_layer(msg,timeout) {
return $.layer.msg(msg,{icon:16,shade:[0.5,‘#f5f5f5‘],scrollbar:false,offset:‘auto‘,time:timeout})
},
closeloading:function prg_close_load_layer(index) {
$.layer.close(index);
},
successMes: function pre_show_tip_layer(){
$.layer.msg("处理完成",{time:300,offset:‘auto‘});
},
get: function (url, param, doFn) {
var options = {};
var exp = new Date();
exp.setTime(exp.getTime() + 40 * 60 * 1000);
options.expires = exp;
var token = gg.core.cookie.getValue("token");
gg.core.cookie.setValue("token",token ,options);
$.ajax({
type: ‘GET‘,
url: url,
data: param,
dataType: "json",
success: function (result) {
if (result.code == 200) {
if (doFn) doFn(result);
}
else if (result.code == 998) {
$.layer.alert(‘用户登录超时,请重新登录!‘, {
icon: 2,
closeBtn: 0
}, function () {
window.location = "login.html";
});
return;
}
else {
$.layer.alert(result.errMsg, {
icon: 2,
closeBtn: 0
});
return;
}
},
error: function () {
$.layer.msg("加载失败",{time:1000,offset:‘auto‘});
},
headers:{
authKey: gg.core.cookie.getValue("token"),
sessionId:gg.core.cookie.getValue("sessionId")
},
});
}
,
post: function (url, param, doFn) {
var options = {};
var exp = new Date();
exp.setTime(exp.getTime() + 40 * 60 * 1000);
options.expires = exp;
gg.core.cookie.setValue("token", gg.core.cookie.getValue("token"),options);
$.ajax({
type: ‘POST‘,
url: url,
data: param,
headers:{
authKey: gg.core.cookie.getValue("token"),
sessionId:gg.core.cookie.getValue("sessionId")
},
dataType: "json",
contentType: "application/json",
success: function (result) {
if (result.code == 200) {
if (doFn) doFn(result);
}
else if (result.code == 998) {
gg.core.cookie.deleteCookid("token")
$.layer.alert(‘用户登录超时,请重新登录!‘, {
icon: 2,
closeBtn: 0
}, function () {
window.location = "login.html";
});
return;
}
else {
$.layer.alert(result.errMsg, {
icon: 2,
closeBtn: 0
});
return;
}
},
error:function(event){
$.layer.msg("加载失败",{time:1000,offset:‘auto‘});
}
});
}
}
以上是关于ajax封装的主要内容,如果未能解决你的问题,请参考以下文章