yunmoxue 发表于 2013-1-29 12:08:12

简单的封装了一下ajax通信..

<script>//a继承bfunction Extend(a,b){for(var pro in b)a = b;}//ajax 类function ajax(opts){this.xhr = false;//默认值this.opts = {method : "get",//请求方式url : "",//请求地址asynch : true ,//是否异步callBack : function(xhr){},//用户的回调函数content : null,//send()方法的参数readyState : 4,//请求的状态,有5个可取值:0 = 未初始化,1 = 正在加载,2 = 已加载,3 = 交互中,4 = 完成status : 200 //服务器的HTTP状态码(200对应OK,404对应Not Found(未找到),等等)}//赋值Extend(this.opts,opts);/*创建XHR对象*/this.createXHR = function(){if(window.XMLHttpRequest)this.xhr = new XMLHttpRequest();else if(window.ActiveXObject)this.xhr = new ActiveXObject("Microsoft.XMLHTTP");};/*发送请求*/this.doRequest = function(){this.xhr.onreadystatechange = (function(ajaxer){return function(){ajaxer.realCall();}})(this);this.xhr.open(this.opts.method,this.opts.url,this.opts.asynch);this.xhr.send(this.opts.content);};/*真正的回调函数*/this.realCall = function(){if(this.xhr == null)return;if(this.xhr.readyState == this.opts.readyState){if(this.xhr.status == this.opts.status){this.opts.callBack(this.xhr);}}};this.createXHR();this.doRequest();}//声明一个ajax参数所需要的参数var getRelateds = {method : "post",//请求方式url : "related.xml",//请求地址asynch : true ,//是否异步callBack : function(xhr){document.write(xhr.responseText);},//用户的回调函数content : null};//创建一个ajax实例..var ajax1 = new ajax(getRelateds);</script>
页: [1]
查看完整版本: 简单的封装了一下ajax通信..