71 lines
2.0 KiB
JavaScript
71 lines
2.0 KiB
JavaScript
|
|
$(function () {
|
|
//继承jquery的$.ajax方法
|
|
var _ajax = $.ajax;
|
|
$.r_ajax = function (opt) {
|
|
//备份opt中error和complete方法
|
|
var fn = {}
|
|
if (opt.loading) {
|
|
fn.beforeSend = ajaxLoading();
|
|
}
|
|
if (opt.url.indexOf('?') > -1)
|
|
opt.url = opt.url + "&ajaxGuid=" + GetGuid();
|
|
else
|
|
opt.url = opt.url + "?ajaxGuid=" + GetGuid();
|
|
fn.complete = opt.complete;
|
|
//扩展增强处理
|
|
var _opt = $.extend(opt, {
|
|
complete: function (xhr, ts) {
|
|
//成功回调方法增强处理
|
|
if (opt.loading)
|
|
ajaxLoadEnd();
|
|
if (fn.complete != null)
|
|
fn.complete(xhr, ts);
|
|
}
|
|
});
|
|
_ajax(_opt);
|
|
};
|
|
|
|
//继承jquery的$.post方法
|
|
var _post = $.post;
|
|
$.r_post = function (url, data, success, dataType) {
|
|
ajaxLoading();//Loading功能
|
|
var n_success = success;
|
|
var r_data = data;
|
|
var r_dataType = dataType || "json";
|
|
if (!isJson(data)) {
|
|
n_success = data;
|
|
r_data = {};
|
|
r_dataType = success;
|
|
}
|
|
var r_success = function (r_data, r_textStatus, r_jqXHR) {
|
|
ajaxLoadEnd();
|
|
n_success(r_data, r_textStatus, r_jqXHR);
|
|
}
|
|
if (url.indexOf('?') > -1)
|
|
url = url + "&postGuid=" + GetGuid();
|
|
else
|
|
url = url + "?postGuid=" + GetGuid();
|
|
_post(url, r_data, r_success, r_dataType);
|
|
};
|
|
|
|
});
|
|
|
|
function isJson(obj) {
|
|
var isjson = typeof (obj) == "object" && Object.prototype.toString.call(obj).toLowerCase() == "[object object]" && !obj.length;
|
|
return isjson;
|
|
}
|
|
function ajaxLoading() {
|
|
layui.use('layer', function () {
|
|
var layer = layui.layer;
|
|
|
|
layer.load(2, { time: 10 * 1000 });
|
|
});
|
|
}
|
|
function ajaxLoadEnd() {
|
|
layui.use('layer', function () {
|
|
var layer = layui.layer;
|
|
|
|
layer.closeAll('loading');
|
|
});
|
|
} |