/*重写Jquery中的ajax 封装壳*/
$(function () {
;(function ($) {
var _ajax = $.ajax
//重写jquery的ajax方法
$.ajax = function (opt) {
var _opt = {
//全局允许跨域
xhrFields: {
withCredentials: true,
},
contentType: 'application/json; charset=UTF-8',
data: opt.params === undefined ? undefined : JSON.stringify(opt.params),
type: opt.type === undefined ? 'POST' : opt.type,
async: opt.async === undefined ? true : opt.async,
cache: opt.cache === undefined ? false : opt.cache,
dataType: opt.dataType === undefined ? 'json' : opt.dataType,
timeout: 60 * 1000,
beforeSend: function (request) {
// 设置头
request.setRequestHeader('token','SDASD65456456556456');
},
success: function (data, textStatus) {
//成功回调方法增强处理
opt.callback(data)
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
//错误方法增强处理 自己根据code提示错误 或跳转
if (opt.failure) {
opt.failure(XMLHttpRequest)
} else {
alert(textStatus)
}
},
complete: function (XHR, TS) {
//请求完成后回调函数 (请求成功或失败之后均调用)。
},
}
var options = $.extend(_opt, opt)
return _ajax(options)
}
})(jQuery)
})