// --------网络操作--------------------
$.HTTP = {
getUrlParam : function(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r != null)
return unescape(r[2]);
return null;
},
// 设置url参数值,ref参数名,value新的参数值
setUrlParam:function (url,ref, value)
{
var str = "";
if (url.indexOf('?') != -1)
str = url.substr(url.indexOf('?') + 1);
else
return url + "?" + ref + "=" + value;
var returnurl = "";
var setparam = "";
var arr;
var modify = "0";
if (str.indexOf('&') != -1) {
arr = str.split('&');
for (i in arr) {
if (arr[i].split('=')[0] == ref) {
setparam = value;
modify = "1";
}
else {
setparam = arr[i].split('=')[1];
}
returnurl = returnurl + arr[i].split('=')[0] + "=" + setparam + "&";
}
returnurl = returnurl.substr(0, returnurl.length - 1);
if (modify == "0")
if (returnurl == str)
returnurl = returnurl + "&" + ref + "=" + value;
}
else {
if (str.indexOf('=') != -1) {
arr = str.split('=');
if (arr[0] == ref) {
setparam = value;
modify = "1";
}
else {
setparam = arr[1];
}
returnurl = arr[0] + "=" + setparam;
if (modify == "0")
if (returnurl == str)
returnurl = returnurl + "&" + ref + "=" + value;
}
else
returnurl = ref + "=" + value;
}
return url.substr(0, url.indexOf('?')) + "?" + returnurl;
}, // 删除参数值
deleteUrlParam:function (url,ref) {
var str = "";
if (url.indexOf('?') != -1) {
str = url.substr(url.indexOf('?') + 1);
}
else {
return url;
}
var arr = "";
var returnurl = "";
var setparam = "";
if (str.indexOf('&') != -1) {
arr = str.split('&');
for (i in arr) {
if (arr[i].split('=')[0] != ref) {
returnurl = returnurl + arr[i].split('=')[0] + "=" + arr[i].split('=')[1] + "&";
}
}
return url.substr(0, url.indexOf('?')) + "?" + returnurl.substr(0, returnurl.length - 1);
}
else {
arr = str.split('=');
if (arr[0] == ref) {
return url.substr(0, url.indexOf('?'));
}
else {
return url;
}
}
},
/**
*
* @param opt
* opt.url,postdata, success, failure, error
*/
obj : function(opt) { var formData; if (opt.postType) {
switch (opt.postType) {
case "multipart":
formData = new FormData($(opt.formId)[0]);
break;
case "form":
if(opt.formId){
formData=$(opt.formId).serialize();
}else{
formData=$.param(opt.ajaxData);
}
if(opt.extData){
formData += "&"+$.param(opt.extData);
}
break;
case "json":
formData = JSON.stringify(opt.ajaxData);
break;
default:
return;
}
} else {
formData = opt.ajaxData;
} var sopt = {
type : opt.type == undefined ? "post" : opt.type,
async : false,
url : opt.url,
data : formData,
dataType : "json",
success : function(json) {
if (json.stat == 1) {
if (opt.success != undefined)
opt.success(json.data);
} else {
if (opt.failure != undefined)
opt.failure(json);
else if (json.code != undefined) {
$.HTTP.show_code_err(json);
} else {
console.log("success不等于true 【url: " + opt.url + "】");
if (json.errorMessages != null
&& json.errorMessages.length > 0) {
console.error("错误", json.errorMessages[0]);
}
} } },
error : function(XMLHttpRequest, textStatus, errorThrown) {
var info = "XMLHttpRequest:" + JSON.stringify(XMLHttpRequest)
+ " ;textStatus:" + textStatus + "; errorThrown:"
+ JSON.stringify(errorThrown) + "; 【" + opt.url + "】";
console.log(info);
if (opt.error != undefined)
opt.error(XMLHttpRequest, textStatus, errorThrown);
else {
console.error("请求错误", "系统发生请求错误,请联系管理员解决。");
}
}
};
if (opt.postType) {
switch (opt.postType) {
case "multipart":
sopt.async = false;
sopt.cache = false;
sopt.contentType = false;
sopt.processData = false;
break;
case "form":
sopt.contentType = 'application/x-www-form-urlencoded';
break;
case "json":
sopt.contentType = 'application/json';
sopt.dataType = "json";
break;
default:
return;
}
}
$.ajax($.extend(sopt, opt.ajaxOption));
},
list : function(opt) { var formData; if (opt.postType) {
switch (opt.postType) {
case "multipart":
formData = new FormData($(opt.formId)[0]);
break;
case "form":
if(opt.formId){
formData=$(opt.formId).serialize();
}else{
formData=$.param(opt.ajaxData);
}
if(opt.extData){
formData += "&"+$.param(opt.extData);
}
break;
case "json":
formData =JSON.stringify(opt.ajaxData);
break;
default:
return;
}
} else {
formData = opt.ajaxData;
} var sopt = {
type : opt.type == undefined ? "get" : opt.type,
async : false,
url : opt.url,
data : formData,
dataType : "json",
success : function(json) {
if (json.stat == 1) {
if (opt.success != undefined)
opt.success(json.list, json.pageInfo);
} else {
if (opt.failure != undefined)
opt.failure(json);
else if (json.code != undefined) {
$.HTTP.show_code_err(json);
} else {
console.log("success不等于true 【url: " + opt.url + "】");
if (json.errorMessages != null
&& json.errorMessages.length > 0) {
console.error("错误", json.errorMessages[0]);
}
}
} },
error : function(XMLHttpRequest, textStatus, errorThrown) {
var info = "XMLHttpRequest:" + JSON.stringify(XMLHttpRequest)
+ " ;textStatus:" + textStatus + "; errorThrown:"
+ JSON.stringify(errorThrown) + "; 【" + opt.url + "】";
console.log(info);
if (opt.error != undefined)
opt.error(XMLHttpRequest, textStatus, errorThrown);
else {
console.error("请求错误", "系统发生请求错误,请联系管理员解决。");
}
}
}; if (opt.postType) {
switch (opt.postType) {
case "multipart":
sopt.async = false;
sopt.cache = false;
sopt.contentType = false;
sopt.processData = false;
break;
case "form":
sopt.contentType = 'application/x-www-form-urlencoded';
break;
case "json":
sopt.contentType = 'application/json';
sopt.dataType = "json";
break;
default:
return;
}
} $.ajax($.extend(sopt, opt.ajaxOption));
},
show_code_err : function(json) {
if (json.code == 1403) {
showLogin();
return;
} else if (json.errorMessages != undefined
&& json.errorMessages != null && json.errorMessages.length > 0) {
alert(json.errorMessages[0]);
console.error("错误", json.errorMessages[0]);
} }
}