ajax二次封装

/**
*@description Ajax请求二次封装 带loading窗口
*/
function Ajax(options) {

var wait_ico;
if(plus != undefined && plus != null) {
wait_ico = plus.nativeUI.showWaiting();
}
mui.ajax('http://' + GetServerInfo() + ((options.url || "").indexOf('/') == 0 ? (options.url || "") : ('/' + options.url || "/")), {
data: options.data || "",
dataType: 'json', //服务器返回json格式数据
type: options.type || 'get', //HTTP请求类型
timeout: options.timeout || 10000, //超时时间设置为10秒;
headers: options.headers || "",
crossDomain:true,
success: function(data) {
wait_ico.close();
console.log(JSON.stringify(data));
//data = data;
//console.log(data.IsError)
if(data.success) {
if(options.success != null || options.success != undefined) {
options.success.call(null, data);
}
} else {
plus.nativeUI.toast(data.msg);
}
},
error: function(xhr, type, errorThrown) {
console.log(123);
if(wait_ico != null && wait_ico != undefined) {
wait_ico.close();
}

if(type == "timeout") {
plus.nativeUI.toast("网络链接超时...");
} else {
plus.nativeUI.toast("数据加载异常,请重试");
}

if(options.error != undefined && options.error != null) {
options.error.call(null, xhr, type, errorThrown)
}
}
});

}

/**
*@description Ajax请求二次封装 无loading窗口
*/
function AjaxAnsyc(options) {
mui.ajax('http://' + GetServerInfo() + ((options.url || "").indexOf('/') == 0 ? (options.url || "") : ('/' + options.url || "/")), {
data: options.data || "",
dataType: 'json', //服务器返回json格式数据
type: options.type || 'get', //HTTP请求类型
timeout: options.timeout || 10000, //超时时间设置为10秒;
success: function(data) {
if(data.success) {
if(options.success != null || options.success != undefined) {
options.success.call(null, data.result);
}
} else {
plus.nativeUI.toast(data.msg);
}
},
error: function(xhr, type, errorThrown) {
if(type == "timeout") {
plus.nativeUI.toast("网络链接超时");
} else {
plus.nativeUI.toast("数据加载异常,请重试");
}
if(options.error != undefined && options.error != null) {
options.error.call(null, xhr, type, errorThrown)
}
}
});

}

posted @ 2020-07-31 16:09  萌橙  阅读(593)  评论(0编辑  收藏  举报