You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
194 lines
7.6 KiB
194 lines
7.6 KiB
/*
|
|
* 跨域执行的ajax
|
|
* @url: request url
|
|
* @callback: callback function
|
|
* @errorhandle: error handle function
|
|
* @timer: request interval
|
|
* @retrial: error request times(value=-1:infinity)
|
|
* var playAjax = new AJAX_OBJ(url, onSuccessHandler,onErrorHandler,2, 1000);
|
|
playAjax.getRequestData();playAjax.postRequestData("");
|
|
onSuccessHandler(data)
|
|
onErrorHandler(data)
|
|
*/
|
|
|
|
|
|
var objPool = [];
|
|
|
|
function AJAX_OBJ(url, callback, errorhandle, timer, retrial) {
|
|
this.xmlHttp = null;
|
|
this.num = 0;
|
|
this.url = url;
|
|
this.urlParameters = "";
|
|
this.callback = callback;
|
|
this.errorhandle = errorhandle;
|
|
|
|
this.timer = timer || 5000;
|
|
this.timeout = -1;
|
|
this.retrial = retrial || 1;
|
|
|
|
this.childnodes = null;
|
|
}
|
|
|
|
/*创建 XMLHttpRequest 对象,IE 浏览器使用 ActiveXObject,而其他的浏览器使用名为 XMLHttpRequest 的 JavaScript 内建对象。*/
|
|
AJAX_OBJ.prototype.createXMLHttpRequest = function() {
|
|
var xmlh = null;
|
|
if (window.ActiveXObject) {
|
|
xmlh = new ActiveXObject("Microsoft.XMLHttp");
|
|
} else if (window.XMLHttpRequest) {
|
|
xmlh = new XMLHttpRequest();
|
|
}
|
|
return xmlh;
|
|
}
|
|
|
|
/*获取一个XMLHttpRequest 对象*/
|
|
AJAX_OBJ.prototype.getInstance = function() {
|
|
for (var i = 0; i < objPool.length; i++) {
|
|
if (objPool[i].readyState == 0 || objPool[i].readyState == 4) { // 返回已有的readyState属性值为0或者4的XMLHttpRequest对象,readyState=0,请求未初始化(在调用 open() 之前);readyState=4,请求已完成(可以访问服务器响应并使用它)
|
|
return objPool[i];
|
|
}
|
|
}
|
|
objPool[objPool.length] = this.createXMLHttpRequest(); //如果已有的XMLHttpRequest对象都属于非空闲状态,则创建返回一个新的XMLHttpRequest对象
|
|
return objPool[objPool.length - 1];
|
|
}
|
|
|
|
/*GET请求数据*/
|
|
AJAX_OBJ.prototype.getRequestData = function(heads) {
|
|
this.xmlHttp = this.getInstance(); //获取一个XMLHttpRequest 对象
|
|
var request_url = this.url + this.urlParameters; //服务器端脚本的 URL
|
|
|
|
// alert(request_url);
|
|
var self = this;
|
|
if (request_url.indexOf("?") > -1) {
|
|
request_url = request_url + "&random=" + Math.random(); //如果XMLHttpRequest提交的URL与历史一样则使用缓存,地址后面加上随机数可以避免使用缓存,取到最新的数据
|
|
} else {
|
|
request_url = request_url + "?random=" + Math.random();
|
|
}
|
|
|
|
this.xmlHttp.onreadystatechange = function() { //每当 readyState 改变时,onreadystatechange 函数就会被执行。
|
|
self.stateChanged();
|
|
};
|
|
|
|
this.xmlHttp.open("GET", request_url, true); //向服务器发送一个请求
|
|
|
|
if (heads == "chaoxing") {
|
|
this.setRequestHeader(heads); //设置请求头
|
|
} else {
|
|
this.setDefaultRequestHeader(heads); //设置请求头
|
|
}
|
|
|
|
|
|
this.xmlHttp.send(null);
|
|
//显示加载
|
|
//showLoading();
|
|
}
|
|
|
|
/*POST请求数据*/
|
|
AJAX_OBJ.prototype.postRequestData = function(param, heads) {
|
|
this.xmlHttp = this.getInstance(); //获取一个XMLHttpRequest 对象
|
|
var request_url = this.url + this.urlParameters; //服务器端脚本的 URL
|
|
//alert(request_url);
|
|
var self = this;
|
|
if (request_url.indexOf("?") > -1) {
|
|
request_url = request_url + "&random=" + Math.random(); //如果XMLHttpRequest提交的URL与历史一样则使用缓存,地址后面加上随机数可以避免使用缓存,取到最新的数据
|
|
} else {
|
|
request_url = request_url + "?random=" + Math.random();
|
|
}
|
|
|
|
this.xmlHttp.onreadystatechange = function() { //每当 readyState 改变时,onreadystatechange 函数就会被执行。
|
|
self.stateChanged();
|
|
};
|
|
|
|
this.xmlHttp.open("POST", request_url, true); //向服务器发送一个请求
|
|
this.setDefaultRequestHeader(heads); //设置请求头
|
|
|
|
if (param == null || param === undefined || param == "") {
|
|
this.xmlHttp.send(null);
|
|
} else {
|
|
this.xmlHttp.send(param);
|
|
}
|
|
//显示加载
|
|
//showLoading();
|
|
}
|
|
|
|
/* 自定义请求头信息,heads对象,key-value形式 */
|
|
AJAX_OBJ.prototype.setDefaultRequestHeader = function(heads) {
|
|
this.xmlHttp.setRequestHeader("Content-type", "application/json;charset=utf-8");
|
|
this.xmlHttp.setRequestHeader("Accept", "application/json;charset=UTF-8");
|
|
this.xmlHttp.setRequestHeader("normal_login_token", cookieHandler.get("normal_login_token"));
|
|
// this.xmlHttp.setRequestHeader("prodCode","fxcloud2.0");
|
|
heads = heads || {};
|
|
for (var key in heads) {
|
|
this.xmlHttp.setRequestHeader(key, heads[key]);
|
|
}
|
|
}
|
|
/*超星图书请求头信息,head对象,key-value形式 */
|
|
AJAX_OBJ.prototype.setRequestHeader = function(heads) {
|
|
this.xmlHttp.setRequestHeader("Content-type", "application/json;charset=utf-8");
|
|
this.xmlHttp.setRequestHeader("Accept", "application/json;charset=UTF-8");
|
|
//this.xmlHttp.setRequestHeader("Content-length",param.length);
|
|
heads = heads || {};
|
|
for (var key in heads) {
|
|
this.xmlHttp.setRequestHeader(key, heads[key]);
|
|
}
|
|
}
|
|
/*每当 readyState 改变时,执行此方法*/
|
|
AJAX_OBJ.prototype.stateChanged = function() {
|
|
if (this.xmlHttp.readyState == 4) { //请求已完成
|
|
if (this.xmlHttp.status == 200) { //请求成功时,调用回到函数callback,将请求到的对象作为函数参数
|
|
//隐藏加载
|
|
//hiddenLoading();
|
|
if (this.xmlHttp.getResponseHeader("normal_login_token") == null) {
|
|
// console.log('--------------token 未过期-------------')
|
|
} else {
|
|
var token = this.xmlHttp.getResponseHeader("normal_login_token");
|
|
cookieHandler.set("normal_login_token", token); //保存最新token
|
|
// 解析token 设置isli
|
|
verifyToken(token);
|
|
}
|
|
this.callback(this.xmlHttp);
|
|
} else { //error handling 请求失败时,再次请求
|
|
//alert("请求失败");
|
|
this.num++;
|
|
//隐藏加载
|
|
//hiddenLoading();
|
|
this.errorhandle(this.xmlHttp.status);
|
|
if (this.retrial != -1 && this.num < this.retrial) {
|
|
clearTimeout(this.timeout);
|
|
//this.timeout = setTimeout(function(){self.requestData();}, this.timer);
|
|
} else if (this.retrial == -1) { //
|
|
var self = this;
|
|
clearTimeout(this.timeout);
|
|
//this.timeout = setTimeout(function(){self.requestData();}, this.timer);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/*向请求地址增加参数*/
|
|
AJAX_OBJ.prototype.addParameter = function(params) {
|
|
if (params.length > 0) {
|
|
this.urlParameters = "";
|
|
for (var i = 0; i < params.length; i++) {
|
|
var curr_param = params[i];
|
|
if (i == 0) this.urlParameters += "?" + curr_param.name + "=" + curr_param.value;
|
|
else this.urlParameters += "&" + curr_param.name + "=" + curr_param.value;
|
|
}
|
|
}
|
|
}
|
|
|
|
/*获取返回来的xml数据的某个节点*/
|
|
AJAX_OBJ.prototype.getNodes = function(param, index) {
|
|
if (typeof(this.xmlHttp.responseXML) != "undefined") {
|
|
if (typeof(param) != "undefined" && typeof(index) != "undefined") {
|
|
this.childnodes = this.xmlHttp.responseXML.getElementsByTagName(param)[index].childNodes;
|
|
return this.childnodes;
|
|
} else if (typeof(param) != "undefined") {
|
|
this.childnodes = this.xmlHttp.responseXML.getElementsByTagName(param);
|
|
return this.childnodes;
|
|
} else {
|
|
return null;
|
|
}
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|