!(function() {
var timeout = 16000;
//设置ajax请求的setting配置----start
jQuery.support.cors = true;
$.extend($.ajaxSettings, {
cache: true,
timeout: timeout, //毫秒
dataType: "json",
type: "POST",
xhrFields: {
withCredentials: true
},
crossDomain: !0
// contentType: "application/x-www-form-urlencoded;charset=UTF-8"
});
//设置ajax请求的setting配置----end
/**
* 判断是否是低于制定版本的ie
*/
window.IsLetIe = function() {
var ua = navigator.userAgent;
return ua.indexOf("MSIE 6") > 0 || ua.indexOf("MSIE 7") > 0 || ua.indexOf("MSIE 8") > 0 || ua.indexOf("MSIE 9") > 0;
}
/**
* ajax封装 默认get
* @param {String} url 地址
* @param {JSON} data 数据
* @param {String} type 数据
* @param {Function} okFn 成功回调
* @param {Function} failFn 失败回调
* @param {Function} errFn 错误回调
* @param {Function} cmpFn 完成回调
*/
window.Ajax = function(url, data, type, okFn, failFn, errFn, cmpFn) {
type = type ? type : 'GET';
//ie9以下 传json string 在IE6,7,8,9下 传json后端获取不到 只能在body中读取流
var contentType = 'application/x-www-form-urlencoded;charset=UTF-8';
if(window.IsLetIe()) {
data = JSON.stringify(data);
contentType = 'text/plain';
}
$.ajax({
url: url,
type: type,
asysc: true,
data: data,
contentType: contentType,
beforeSend: function(request) {},
success: function(response) {
if(response.responseCode == '001') {
typeof okFn == 'function' && okFn(response);
} else {
typeof failFn == 'function' && failFn(response);
}
},
error: function(xhr, msg, err) {
//alert('xhr-->' + JSON.stringify(xhr))
//alert('msg-->' + msg)
//alert('err-->' + JSON.stringify(err))
if(typeof errFn == "function" && errFn) {
errFn(xhr);
} else {
alert('请求失败,请稍后重试');
}
},
complete: function(response) {
typeof cmpFn == "function" && cmpFn();
}
});
}
/**
* post请求
* @param {String} url 地址
* @param {JSON} data 数据
* @param {Function} okFn 成功回调
* @param {Function} failFn 失败回调
* @param {Function} errFn 错误回调
* @param {Function} cmpFn 完成回调
*/
window.AjaxPost = function(url, data, okFn, failFn, errFn, cmpFn) {
Ajax(url, data, 'POST', okFn, failFn, errFn, cmpFn);
}
/**
* get请求
* @param {String} url 地址
* @param {JSON} data 数据
* @param {Function} okFn 成功回调
* @param {Function} failFn 失败回调
* @param {Function} errFn 错误回调
* @param {Function} cmpFn 完成回调
*/
window.AjaxGet = function(url, data, okFn, failFn, errFn, cmpFn) {
Ajax(url, data, 'GET', okFn, failFn, errFn, cmpFn);
}
var _app = {
config: {
environment: 'prod' //当前接口环境
},
domain: {
qirong: {
prod: baseURL + '/'
}
},
source: {
inter: {
getInterInfo: 'into/getInterInfo.do'
}
},
api: {
getInterInfo: function(inputData, okFn, failFn, errFn, cmpFn) {
AjaxPost(_app.source.inter.getInterInfo, inputData, okFn, failFn, errFn, cmpFn);
}
},
init: function() {
//加载接口
this.load();
window.Api = _app.api;
},
load: function() {
//遍历source 获取所有类型名称 inner,ocrweb....
for(var type in this.source) {
//遍历source[type]当前的类型下的接口地址 例source.inner.creatImg
for(var item in this.source[type]) {
//将当前项的地址拼根据当前接口环境拼接上主域名
this.source[type][item] = [this.domain[type][this.config.environment], this.source[type][item]].join('');
}
}
}
}
_app.init();
})();