日期:2014-05-16 浏览次数:20614 次
ajax使用
/** ajax的使用*/ function displayMessage(){ var ajaxUrl="toPlay!getTopTwoAssetBean.action"; //ajax访问的地址 rtspAjax = new AJAX_OBJ(ajaxUrl,responseFunction); //调用我们ajax.js中的代码 responseFunction为ajax回调函数 rtspAjax.errorhandle=RequestError; //调用ajax异常时,处理函数 rtspAjax.requestData(); } /**处理ajax请求返回数据*/ function responseFunction(){ var txt = rtspAjax.getText(); alert(txt); //打印出ajax请求的内容 } /**处理ajax请求失败函数*/ function RequestError(){ alert("获取数据超时,请检查网络"); }?
?ajax.js
/*============================================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() { this.xmlHttp.open("POST", this.url, this.asyn); var self = this; this.xmlHttp.onreadystatechange = function() { self.stateChanged(); } 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 t