六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 29|回复: 0

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

[复制链接]

升级  11.67%

69

主题

69

主题

69

主题

举人

Rank: 3Rank: 3

积分
235
 楼主| 发表于 2013-1-23 02:14:33 | 显示全部楼层 |阅读模式
<script>//a继承bfunction Extend(a,b){for(var pro in b)a[pro] = b[pro];}//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>
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表