方法一
postData: function (params) {
var postParm = {
murl: '', // 请求url
mdata: {}, // 请求参数
timeout: 30000, // 请求超时时间,默认30秒
successCallBack: '' // 成功回调函数
}
postParm = $.extend(postParm, params);
// mdata = mdata ? mdata : "";
// if (typeof mdata == "object") {
// for (var a in mdata) {
// if (typeof mdata[a] == "string") {
// mdata[a] = mdata[a].replace(/(^\s*)|(\s*$)/g, "");
// }
// }
// }
var options = {
type: "POST",
timeout: postParm.timeout,
url: base.baseURL + postParm.murl,
dataType: "json",
data: postParm.mdata
}
return $.ajax(options).then(function (response) {
if (response.status == 400) {
base.notification({
type: 'error',
message: response.msg,
duration: 3000
});
setTimeout(function () {
window.location.href = base.baseURL + "login.jsp";
}, 3000);
return false;
}
if (response.status == 300) {
base.notification({
type: 'error',
message: response.msg,
duration: 3000
});
setTimeout(function () {
window.location.href = base.baseURL + "index/index.jsp";
}, 3000);
return false;
}
if (response && response.length > 0 || response && response.success) {
postParm.successCallBack ? postParm.successCallBack() && base.notification({
message: response.msg,
duration: 3000,
title: '成功提示'
}) : '';
return response;
}
if (response && response.success == false) {
if (response.message || response.msg) {
base.notification({
type: 'error',
message: response.msg || response.message,
duration: 3000
});
}
return response
}
return response
});
},
用法
base.postData({
murl: 'sys/user/list',//接口
mdata: {//参数
pageNumber: 1,
pageSize: 1
}
}).then(function (res) {
});
方法二
postJson: function (murl, mdata, successCallBack, errCallback) {
// mdata = mdata ? JSON.stringify(mdata) : "";
var options = {
contentType: "application/x-www-form-urlencoded;charset=UTF-8",
type: "POST",
timeout: 30000,
url: base.baseURL + murl,
dataType: "json",
data: mdata,
success: function (e) {
if (e.status == 400) {
base.notification({
type: 'error',
message: e.msg,
duration: 3000
});
setTimeout(function () {
window.location.href = base.baseURL + "login.jsp";
}, 3000);
return false;
}
if (e.status == 300) {
base.notification({
type: 'error',
message: e.msg,
duration: 3000
});
setTimeout(function () {
window.location.href = base.baseURL + "index/index.jsp";
}, 3000);
return false;
}
if (e && e.length > 0 || e && e.success) {
successCallBack ? successCallBack(e) : function () {}
} else {
errCallback ? errCallback(e) : base.notification({
type: 'error',
message: e.msg,
duration: 3000
});
}
},
error: function (xhr, type, errorThrown) {
errCallback ? errCallback() : base.notification({
type: 'error',
message: type == 'timeout' ? '请求超时' : '请求异常',
duration: 3000
});
}
};
$.ajax(options);
},
用法
base.postJson('infoSta/devCallRefusal', { // 拒绝接受,通知设备显示“总台繁忙,请稍后重试”
devCode: data.devCode
}, function (result) {
console.log("send devCallRefusal success");
}, function (err) {
});