Ajax封装
/**
* 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);
}); 相关推荐
kentrl 2020-11-10
结束数据方法的参数,该如何定义?-- 集合为自定义实体类中的结合属性,有几个实体类,改变下标就行了。<input id="add" type="button" value="新增visitor&quo
ajaxyan 2020-11-09
zndy0 2020-11-03
学留痕 2020-09-20
learningever 2020-09-19
chongxiaocheng 2020-08-16
ajaxhe 2020-08-16
lyqdanang 2020-08-16
curiousL 2020-08-03
时光如瑾雨微凉 2020-07-19
坚持着执着 2020-07-16
jiaguoquan00 2020-07-07
李永毅 2020-07-05
坚持着执着 2020-07-05