/**
* Ajax封装
*/
var Ajax = function(){
this.isIE = !+ [ 1, ] ? true : false;
this.xmlHttpList = [];
};
Ajax.prototype = {
ajaxConnection : 0,
getXmlHttp : function( count ){
count = count != undefined ? count : 5;
var bool = this.xmlHttpList.length === 0,i;
var length = bool ? count : this.xmlHttpList.length;
for(i=0;i<length;i++){
if(bool){
this.xmlHttpList.push(this.isIE ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest());
}else{
if(this.ajaxConnection > length){//高并发时候,少了就添加连接
this.xmlHttpList.push(this.isIE ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest());
i = length;
break;
}
if(this.xmlHttpList[i].readyState === 0)break;
}
}
return this.xmlHttpList[ bool ? 0 : i ];
},
post : function(url,yesFun,noFun){
this.ajax("post",url,yesFun,noFun);
},
get : function(url,yesFun,noFun){
this.ajax("get",url,yesFun,noFun);
},
ajax : function(type,url,yesFun,noFun){
var xmlHttp = this.getXmlHttp();
xmlHttp.open(type, url, true);
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
if( yesFun && typeof yesFun === 'function' ){
yesFun( xmlHttp.responseText );
}
xmlHttp.abort();
Ajax.ajaxConnection --;
}else{
if (xmlHttp.readyState == 4 && xmlHttp.status != 200) {
if( noFun && typeof noFun === 'function' ){
noFun( xmlHttp.readyState );
}
xmlHttp.abort();
Ajax.ajaxConnection --;
}
}
};
xmlHttp.send(null);
}
};
//使用方法
var ajax = new Ajax();
ajax.post("?GchartID="+id,function(html){
alert(html);
},function(state){
alert("请求错误:"+state);
});
Ajax封装
最新推荐文章于 2024-08-02 13:55:36 发布