因为项目没有使用框架所以自己封装了一个ajax请求,同时包含了错误提示。
// 声明一个全局Http请求
window.rydHttpServer = {
/**
* [httpServer 请求服务]
* @param {[type]} url [请求路径]
* @param {[type]} params [请求参数]
* @param {[type]} setDom [load请求的数据]
* @param {[type]} methods [请求方式]
* @return {[type]} [返回结果]
*/
httpServer: function(url, params ,setDom,methods) {
if (params == undefined) { params = {}; }
var deffer = $.Deferred();
if (setDom&&methods == undefined) {
$(setDom).load(url, $.param(params), function(response, status, xhr) {
deffer.resolve(response, status, xhr);
});
return deffer.promise();
} else {
if (params == undefined) { params = {}; }
if(methods){methods = "GET";}
$.ajax({
url: url, // 请求接口
data: $.param(params),
type: methods||'POST',
cache: false,
success: function(data) {
if(data.httpCode != undefined && data.httpCode != 200){
rydHttpServer.alert(data.msg,3);
deffer.reject(data);
}
deffer.resolve(data);
},
error: function(data) {
deffer.reject(data);
console.log("数据异常,信息如下:",data);
}
});
return deffer.promise();
}
},
alert:function(title,time,fun){
if(time == undefined){time =3;}
var tipWinObj = document.createElement("div");
tipWinObj.id = 'window-alert';
tipWinObj.style.cssText = "position:relative;z-index:9999;display: table; position: fixed;left: 0;top: 0;width: 100%;height: 100%;";
var topContent = document.createElement("div");
topContent.style.cssText = "display: table-cell;vertical-align: middle;text-align: center;";
var topDiv = document.createElement("div");
topDiv.style.cssText = " width: auto;max-width: 90%;margin: 0px auto;bottom: -150px;background-color: rgba(0, 0, 0, 0.7);color: rgb(255, 255, 255);position: relative;display: inline-block;text-align: left;border-radius: 5px;box-shadow: 0 0 8px rgba(0,0,0,.1);pointer-events: auto;";
var titDiv = document.createElement("div");
titDiv.className = "tips-text";
titDiv.style.cssText = "padding: 10px 20px;text-align: center;font-size: 0.36rem;";
titDiv.innerHTML = title;
topDiv.appendChild(titDiv);
topContent.appendChild(topDiv);
tipWinObj.appendChild(topContent);
document.body.appendChild(tipWinObj);
var currTime = setTimeout(function (argument) {
document.body.removeChild(document.getElementById("window-alert"));
clearTimeout(currTime);
if(fun != undefined)fun();
},1000*time);
},
/**
* [getUrlParams 获取Url参数]
* @param {[type]} name [需要获取的参数名]
* @return {[type]} [description]
*/
getUrlParams: function(name) {
var url = decodeURI(location.href);
var paraString = url.substring(url.indexOf("?") + 1, url.length).split("&");
var paraObj = {};
var j;
for (j in paraString) {
paraObj[paraString[j].substring(0, paraString[j].indexOf("=")).toLowerCase()] = paraString[j].substring(paraString[j].indexOf("=") + 1, paraString[j].length);
}
var returnValue = paraObj[name.toLowerCase()];
if (typeof(returnValue) == "undefined") {
return "";
} else {
return returnValue;
}
},
/**
* [open 页面跳转]
* @param {[type]} params [description]
* @return {[type]} [description]
*/
open: function(url) {
window.location.href=url;
},
/**
* [setData 处理后台返回null问题]
* @param {[type]} params [description]
*/
removeNull: function(params) {
if (params == undefined) return;
if (params.constructor == Object) {
Object.keys(params).forEach(function(obj) {
if (params[obj] == null || undefined) {
params[obj] = "";
}
});
}
if (params.constructor == Array) {
for(var i = 0; i< params.length;i++){
Object.keys(params[i]).forEach(function(j) {
if (params[i][j] == null || undefined) {
params[i][j] = "";
}
});
}
}
},
/**
* [readCookie 获取cookie]
* @param {[type]} key [description]
* @return {[type]} [description]
*/
readCookie:function(key) {
var nameEQ = key + "=";
var ca = document.cookie.split(';');
for (var i = 0, max = ca.length; i < max; i++) {
var c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1, c.length);
}
if (c.indexOf(nameEQ) === 0) {
return c.substring(nameEQ.length, c.length);
}
}
return "";
}
};