//ajax请求封装
/**
*
* @param host 访问的url
* @param method 访问方式
* @param data data数据
* @param success 成功回调
* @param error 成功回调
* @param complete
*/
function ajaxRequest(param) {
var defaultFun = function (result) {
console.log(result);
}
let host = param.host; // 请求地址
let searchDomin = new RegExp('http|https');
let url = searchDomin.test(host) ? host : window.location.origin + '/' + host; //真实请求地址
let method = param.method; //请求方法
let data = param.data; //请求参数
let success = param.hasOwnProperty("success") ? param.success : defaultFun; //成功回调
let error = param.hasOwnProperty("error") ? param.error : defaultFun; //失败回调
let complete= param.hasOwnProperty("complete") ? param.complete : defaultFun; //失败回调
$.ajax({
url: url,
dataType: 'json',
type: method,
data: data,
success: function (result) {
return success(result);
},
error: function (xhr, type, errorThrown) {
//异常处理;
// if (adminDebug) {
console.log('%crequest fail!', ';color:#dd4b39');
console.log();
console.log("type:" + type + ",readyState:" + xhr.readyState + ",status:" + xhr.status);
console.log("url:" + url);
console.log("data:");
console.log(data);
// layer.close(loadT);
// }
layer.msg('访问错误,代码' + xhr.status, {
icon: 2,
scrollbar: false,
});
return error(xhr);
},
complete:function(xhr, type, errorThrown) {
return complete(type);
}
});
}