//方案一
Admin::script(
<<<JS
(function ($) {
//备份jquery的ajax方法
var _ajax = $.ajax;
//重写jquery的ajax方法
$.ajax = function (opt) {
//备份opt中error和success方法
var fn = {
error: function (XMLHttpRequest, textStatus, errorThrown) { },
success: function (data, textStatus) { }
}
// var sessionuserid = "";
// if (typeof parent.getSessionID == "function") {
// sessionuserid = parent.getSessionID();
// }
let query_arr = opt.data.split("&");
let result = {};
query_arr.map(function(value) {
let query = value.split("=");
result[query[0]] = query[1];
})
opt.data = '';
for (let key in result) {
opt.data += key+'='+result[key]+'&';
}
opt.data = opt.data.substr(0, opt.data.length - 1);
console.log(opt.data)
if (opt.data && !opt.extraData) { //普通ajax提交
// opt.data.sessionuserid = sessionuserid;
console.log('===================ajax=====>>>>>>>'+opt.data)
fn.data = opt.data;
} else if (opt.data && opt.extraData) { //ajaxSubmit提交
// opt.data = "sessionuserid=" + sessionuserid + "&" + opt.data;
// console.log('===================ajaxSubmit=====>>>>>>>'+opt.data)
// opt.extraData.sessionuserid = sessionuserid;
}
if (opt.error) {
fn.error = opt.error;
}
if (opt.success) {
fn.success = opt.success;
}
//扩展增强处理
var _opt = $.extend(opt, {
error: function (XMLHttpRequest, textStatus, errorThrown) {
//错误方法增强处理
fn.error(XMLHttpRequest, textStatus, errorThrown);
},
success: function (data, textStatus) {
//成功回调方法增强处理
fn.success(data, textStatus);
}
});
_ajax(_opt);
};
})(jQuery);
JS
);
//方案二:
Admin::js('js/ajaxhook.min.js');
Admin::script(
<<<JS
//启用前必须销毁,否则报错
ah.unProxy();
ah.proxy({
//请求发起前进入
onRequest: (config, handler) => {
// console.log(config.url)
console.log(config)
if(config.body == null){
if (config.url.indexOf('&') >0) {
let url_arr = config.url.split("?");
config.url = url_arr[0]+'?'+format_query(url_arr[1]);
}
}else{
if ((typeof config.body=='string') && config.body.constructor==String && config.body.indexOf('&') >0) {
config.body = format_query(config.body);
}
}
handler.next(config);
},
//请求发生错误时进入,比如超时;注意,不包括http状态码错误,如404仍然会认为请求成功
one rror: (err, handler) => {
handler.next(err)
},
//请求成功后进入
onResponse: (response, handler) => {
handler.next(response)
}
})
function format_query(s) {
let query_arr = s.split("&");
let result = {};
query_arr.map(function(value) {
let query = value.split("=");
result[query[0]] = query[1];
})
s = '';
for (let key in result) {
s += key+'='+result[key]+'&';
}
return s.substr(0, s.length - 1);
}
JS
);
标签:opt,body,Admin,data,url,ajax,Dcat,query,config
From: https://www.cnblogs.com/imsugar/p/17225962.html