您的位置:首页 > 理论基础 > 计算机网络

程序ajax请求公共组件-- app-jquery-http.js

2016-11-04 09:38 393 查看
$.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;
},
/**
*
* @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":
formData =  $.param(opt.ajaxData);
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)
{
$.NOTIFY.showNotice ("错误", 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
{
$.NOTIFY.showError ("请求错误", "系统发生请求错误,请联系管理员解决。");
}
}
};

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 (sopt);
},
list : function (opt)
{
var formData;

if (opt.postType) {
switch (opt.postType) {
case "multipart":
formData = new FormData($(opt.formId)[0]);
break;
case "form":
formData =  $.param(opt.ajaxData);
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 : opt.ajaxData,
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)
{
$.NOTIFY.showNotice ("错误", 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
{
$.NOTIFY.showError ("请求错误", "系统发生请求错误,请联系管理员解决。");
}
}
};

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 (sopt);
},
show_code_err : function (json)
{
if (json.code == 1403)
{
$.NOTIFY.showLogin ();
return;
}
else if (json.errorMessages != undefined && json.errorMessages != null && json.errorMessages.length > 0)
{
$.NOTIFY.showNotice ("错误", json.errorMessages[0]);
}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐