当前位置: 代码迷 >> Ajax >> 打包ajax
  详细解决方案

打包ajax

热度:175   发布时间:2016-05-11 09:10:22.0
封装ajax
/*-----------ajax-----------------------*//*============================================AJAX 对象==================================* 作用:通过get或post的方式请求异步(同步)数据* @param{string} url: 将要去请求的路径* @param{function} callback: 回调函数,即请求成功时所做的操作,带一个参数:new XMLHttpRequest()的返回值 * @param{boolean} asyn: 是否异步调用,默认为true:异步,反之同步调用* @param{number} timer: 请求失败时,隔timer时间重新请求一次* @param{number} retrieval: 请求失败时,重新请求的次数,当为 -1 时,表示无数次* @param{function} errorhandle: 请求失败时,所做的处理*/var objPool = [];objPool[0] = createXMLHttpRequest();objPool[1] = createXMLHttpRequest();objPool[2] = createXMLHttpRequest();objPool[3] = createXMLHttpRequest();objPool[4] = createXMLHttpRequest();objPool[5] = createXMLHttpRequest();function AJAX_OBJ(url, callback, asyn, timer, retrieval, errorhandle){	this.num = 0;	this.url = url;	this.urlParameters = "";	this.callback = callback;	this.errorhandle = errorhandle || this.errorHandle;	this.timer = timer || 20000;	this.timeout = -1;	this.retrieval = retrieval || 1;		this.asyn = typeof asyn =="undefined"?true:asyn;	this.xmlHttp = this.getInstance();	this.backParam = null;	this.timerout;}//请求实例AJAX_OBJ.prototype.getInstance = function(){	/*var xmlh = null;	xmlh = new XMLHttpRequest();	return xmlh;*/	for (var i = 0; i < objPool.length; i ++)	{		if ( objPool[i].readyState == 4||objPool[i].readyState == 0)		{			return objPool[i];		}	}	objPool[objPool.length] = createXMLHttpRequest();	return objPool[objPool.length - 1];}function createXMLHttpRequest(){	var xmlh = null;	if(window.XMLHttpRequest){		xmlh = new XMLHttpRequest();	}else if(window.ActiveXObject){		xmlh = new ActiveXObject("Microsoft.XMLHTTP");	}	return xmlh;}//用GET方式请求数据AJAX_OBJ.prototype.requestData = function(){	var request_url = this.url + this.urlParameters;	var self = this;	this.xmlHttp.onreadystatechange = function(){		self.stateChanged();	};	this.xmlHttp.open("GET", request_url, this.asyn);	this.xmlHttp.send(null);	}//用POST方式请求数据AJAX_OBJ.prototype.requestDataPost = function() {	var self = this;	this.xmlHttp.onreadystatechange = function() {		self.stateChanged();	}	this.xmlHttp.open("POST", this.url, this.asyn);	this.xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");	this.xmlHttp.setRequestHeader("Content-length", this.urlParameters.length);	this.xmlHttp.setRequestHeader("Connection", "close");	this.xmlHttp.send(this.urlParameters);	return 0;}//请求数据的处理AJAX_OBJ.prototype.stateChanged = function(){	var self=this;	if(this.xmlHttp.readyState == 2) this.timerout=setTimeout(function(){self.check()}, this.timer);	if(this.xmlHttp.readyState == 4){		if(this.xmlHttp.status == 200){			clearTimeout(this.timerout);			if(this.backParam == null) this.callback(this.xmlHttp);			else this.callback(this.xmlHttp,this.backParam);		}	}}AJAX_OBJ.prototype.check = function(){	if(this.xmlHttp.readyState!=4||(this.xmlHttp.readyState==4&&this.xmlHttp.status!=200))		this.errorhandle(this.xmlHttp.status);}//请求失败的处理AJAX_OBJ.prototype.errorHandle = function(params){	shsowTip("请求失败");}//参数的处理AJAX_OBJ.prototype.addParameter = function(params,type){	this.type =  typeof(type)=="undefined"?0:type;	if(this.type == 0){		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;			}		}	}else if(this.type == 1){		this.urlParameters = params;	}}AJAX_OBJ.prototype.getText = function(){	return this.xmlHttp.responseText;}//执行responseTextAJAX_OBJ.prototype.evalText = function(){	return eval(this.xmlHttp.responseText);}
?
  相关解决方案
本站暂不开放注册!
内测阶段只得通过邀请码进行注册!
 
  • 最近登录:Mon Jan 23 12:22:22 CST 2017
  • 最近登录:Mon Jan 23 12:22:22 CST 2017
  • 最近登录:Mon Jan 23 12:22:22 CST 2017
  • 最近登录:Mon Jan 23 12:22:22 CST 2017
  • 最近登录:Mon Jan 23 12:22:22 CST 2017