【发布时间】:2015-08-07 02:38:37
【问题描述】:
我有运行相同 jquery 的 ShowExpired() 和 SessionDestroy() 函数。
唯一的区别:我在 ShowExpired() 中有 if 语句。
我们如何缩小它?
function ShowExpired() {
if (isextend == false) {
$.ajax({
type: "POST",
cache: false,
url: "../../html/frmLogout.aspx/Sessionlogout",
data: "{userid:" + userid + "}",
contentType: 'application/json; charset=utf-8',
dataType: "json",
async: false,
success: function (data, e, jqxhr) {
if (data.d.result) {
window.location.href = '../HTML/frmLogin.aspx';
}
},
error: function (data, e, jqxhr) { alert("logout ERROR=" + data.responseText); }
});
}
}
function SessionDestroy() {
$.ajax({
type: "POST",
cache: false,
url: "../../html/frmLogout.aspx/Sessionlogout",
data: "{userid:" + userid + "}",
contentType: 'application/json; charset=utf-8',
dataType: "json",
async: false,
success: function (data, e, jqxhr) {
if (data.d.result) {
window.location.href = '../HTML/frmLogin.aspx';
}
},
error: function (data, e, jqxhr) { alert("logout ERROR=" + data.responseText); }
});
}
【问题讨论】:
-
如果两个函数都做同样的事情,那你为什么需要检查
if?因为 ajax 部分没有像url、data等那样发生变化。那么为什么你需要if声明呢?