=波波日志 > JavaScript/Ajax > ajax对象应用程序池=
ajax对象应用程序池
此类库简化了创建Ajax对象和使用的过程,不需要自己手动设置状态转换函数onreadystatechange,只需要在使用Showbo.Ajax.send的配置【json对象】中设置成功【success】和失败【failure】的回调函数,主要参数及说明看下面代码的注释内容。
更新说明=======
1)此版本已经修正了火狐下XMLHttpRequest重用时nsIXMLHttpRequest.send发生错误的解决方案,问题描述及解决方案看这篇文章
firefox下XMLHttpRequest重用时nsIXMLHttpRequest.send发生错误的解决方案
代码下载点这里
相关文章
ajax问题总结
javascript如何解析xml文件,兼容ie及ff
更新说明=======
1)此版本已经修正了火狐下XMLHttpRequest重用时nsIXMLHttpRequest.send发生错误的解决方案,问题描述及解决方案看这篇文章
firefox下XMLHttpRequest重用时nsIXMLHttpRequest.send发生错误的解决方案
代码下载点这里
相关文章
ajax问题总结
javascript如何解析xml文件,兼容ie及ff
+展开
-JavaScript
String.prototype.trim=function(){return this.replace(/$\s*|\s*$/g,'');}
var Showbo={author:'showbo',homepage:'http://www.code-design.cn'};
//获取json对象
Showbo.getJson=function(v){if(typeof(v)=='string')return eval('('+v+')');else return v;}
//根据id获取对象
Showbo.$=function(Id){if('object'==typeof(Id))return Id;else if('string'==typeof(Id))return document.getElementById(Id);else return null;}
Showbo.IsIE=!!document.all;
//扩展IE下的XMLHttpRequest
if(Showbo.IsIE&&!window.XMLHttpRequest)window.XMLHttpRequest=function(){
var acX=['msxml2.xmlhttp.5.0','msxml2.xmlhttp.4.0','msxml2.xmlhttp.3.0','msxml2.xmlhttp','microsoft.xmlhttp'],Xhr;
for(var i=0;i<acX.length;i++)try{Xhr=new ActiveXObject(acX[i]);return Xhr;}catch(e){}
return false;
}
//ajax应用池
Showbo.Ajax={
pools:[]//注意pools存储的对象为{xhr:ajax对象,status:ajax的状态},其中ajax的状态为1/0,1表示在使用中,0表示readyState==4了并且执行了回调函数
,getObject:function(){
for(var i=0;i<this.pools.length;i++)if(this.pools[i].status===0){
this.pools[i].status=1;//设置为使用状态
this.pools[i].xhr.onreadystatechange=function(){}//删除状态转换函数
this.pools[i].xhr.abort();//调用abort
return this.pools[i];
}
var xhr=new XMLHttpRequest();
if(xhr.readyState==null){//更正某些Mozilla浏览器无readyState的问题
xhr.readyState=0;
xhr.addEventListener("load",function(){
xhr.readyState=4;
if(typeof(xhr.onreadystatechange)=="function")xhr.onreadystatechange();
},false);
}
this.pools[this.pools.length]={xhr:xhr,status:1};
return this.pools[this.pools.length-1];
}
,send:function(cfg){/*cfg示例
{
url:'请求的页面'
,params:'键值对,注意不是json对象'
,method:'post/get,如果为指定则默认为get'
,success:成功时的回调函数
,failure:失败时的回调函数
,otherParams:提供给回调函数的第二个参数,可以为json对象
}
成功或者失败的回调函数参数为 (当前的xhr对象,配置文件的中的otherParams)
*/
if(!cfg||!cfg.url)throw("url不正确!");
var method=cfg.method,asy="boolean"==typeof(cfg.asy)?cfg.asy:true;
if(!method)method="get";
if(method.toLocaleLowerCase()=='get'){
var _dc=new Date().getTime();
cfg.params=cfg.params?cfg.params+'&_dc='+_dc:'_dc='+_dc;
if(cfg.url.indexOf("?")!=-1)cfg.url+="&"+cfg.params;
else cfg.url+="?"+cfg.params;
cfg.params=null;
}
else if(typeof(cfg.params)=="undefined")cfg.params='';
var o=this.getObject();//注意并非实际的xhr对象
if(!o.xhr)throw("未能创建ajax对象!");
o.xhr.open(method,cfg.url,asy);
if(method.toLocaleLowerCase()=='post')o.xhr.setRequestHeader("content-type","application/x-www-form-urlencoded");
o.xhr.onreadystatechange=function(){
if(o.xhr.readyState==4){
if(o.xhr.status==200||o.xhr.status==0){
if("function"==typeof(cfg.success))cfg.success(o.xhr,cfg.otherParams);
}
else if("function"==typeof(cfg.failure))cfg.failure(o.xhr,cfg.otherParams);
o.status=0;//=============更改状态为未使用
}
}
o.xhr.send(cfg.params);
}
}
var Showbo={author:'showbo',homepage:'http://www.code-design.cn'};
//获取json对象
Showbo.getJson=function(v){if(typeof(v)=='string')return eval('('+v+')');else return v;}
//根据id获取对象
Showbo.$=function(Id){if('object'==typeof(Id))return Id;else if('string'==typeof(Id))return document.getElementById(Id);else return null;}
Showbo.IsIE=!!document.all;
//扩展IE下的XMLHttpRequest
if(Showbo.IsIE&&!window.XMLHttpRequest)window.XMLHttpRequest=function(){
var acX=['msxml2.xmlhttp.5.0','msxml2.xmlhttp.4.0','msxml2.xmlhttp.3.0','msxml2.xmlhttp','microsoft.xmlhttp'],Xhr;
for(var i=0;i<acX.length;i++)try{Xhr=new ActiveXObject(acX[i]);return Xhr;}catch(e){}
return false;
}
//ajax应用池
Showbo.Ajax={
pools:[]//注意pools存储的对象为{xhr:ajax对象,status:ajax的状态},其中ajax的状态为1/0,1表示在使用中,0表示readyState==4了并且执行了回调函数
,getObject:function(){
for(var i=0;i<this.pools.length;i++)if(this.pools[i].status===0){
this.pools[i].status=1;//设置为使用状态
this.pools[i].xhr.onreadystatechange=function(){}//删除状态转换函数
this.pools[i].xhr.abort();//调用abort
return this.pools[i];
}
var xhr=new XMLHttpRequest();
if(xhr.readyState==null){//更正某些Mozilla浏览器无readyState的问题
xhr.readyState=0;
xhr.addEventListener("load",function(){
xhr.readyState=4;
if(typeof(xhr.onreadystatechange)=="function")xhr.onreadystatechange();
},false);
}
this.pools[this.pools.length]={xhr:xhr,status:1};
return this.pools[this.pools.length-1];
}
,send:function(cfg){/*cfg示例
{
url:'请求的页面'
,params:'键值对,注意不是json对象'
,method:'post/get,如果为指定则默认为get'
,success:成功时的回调函数
,failure:失败时的回调函数
,otherParams:提供给回调函数的第二个参数,可以为json对象
}
成功或者失败的回调函数参数为 (当前的xhr对象,配置文件的中的otherParams)
*/
if(!cfg||!cfg.url)throw("url不正确!");
var method=cfg.method,asy="boolean"==typeof(cfg.asy)?cfg.asy:true;
if(!method)method="get";
if(method.toLocaleLowerCase()=='get'){
var _dc=new Date().getTime();
cfg.params=cfg.params?cfg.params+'&_dc='+_dc:'_dc='+_dc;
if(cfg.url.indexOf("?")!=-1)cfg.url+="&"+cfg.params;
else cfg.url+="?"+cfg.params;
cfg.params=null;
}
else if(typeof(cfg.params)=="undefined")cfg.params='';
var o=this.getObject();//注意并非实际的xhr对象
if(!o.xhr)throw("未能创建ajax对象!");
o.xhr.open(method,cfg.url,asy);
if(method.toLocaleLowerCase()=='post')o.xhr.setRequestHeader("content-type","application/x-www-form-urlencoded");
o.xhr.onreadystatechange=function(){
if(o.xhr.readyState==4){
if(o.xhr.status==200||o.xhr.status==0){
if("function"==typeof(cfg.success))cfg.success(o.xhr,cfg.otherParams);
}
else if("function"==typeof(cfg.failure))cfg.failure(o.xhr,cfg.otherParams);
o.status=0;//=============更改状态为未使用
}
}
o.xhr.send(cfg.params);
}
}
类别:JavaScript/Ajax 作者:波波 日期:2009-07-02 【评论:2】
- 辉仔
日期:2009-7-10 18:07:09 IP:222.247.*.*
老大,你叫我来看你这个ajax对象应用池,我是看了,但看不出来什么明堂。这个东西有什么用处总得介绍一下吧,你是高手,但是总得顾及我们这些菜鸟的理解能力吧管理员回复(2009-7-14 19:02:48)
内容说明已更新
- anya
日期:2009-12-30 13:33:06 IP:125.122.*.*
就是一个创建ajax对象的工厂啊管理员回复(2009-12-30 16:08:03)
便于重用~不需要每次都创建。
发表留言
百度赞助
同类热门博文
- ·AJAX跨域问题解决办..
- ·ajax问题总结
- ·jQuery dataType指定..
- ·ajax+asp.net+mssql..
- ·ajax无刷新上传文件..
- ·ajax对象abort方法
- ·JavaScript代码,变..
- ·fckeditor编辑器在F..
博格Tag
- flash/flex/fcs/AIR(752)
- Asp.Net/C#/WCF(595)
- 操作系统及应用软件(376)
- JavaScript/Ajax(330)
- SQL及数据库(134)
- 黑客技术(115)
- Asp/VBScript(111)
- HTML/WML/CSS兼容/XML(102)
- PHP/apache/Perl(96)
- 网站排名及优化(92)
- 其他(75)
- 个人日志(66)
- lucene.net/分词技术(33)
- 计算机网络(26)
- 机械重工(26)
- C#设计模式(24)
- Google Maps开发(17)
- 日语学习(15)
- Canvas/VML/SVG(13)
- linux(11)
- 游戏开发(8)
- 正则表达式(5)
- Jsp/Java(4)
最新博文
- ·javascript实现html..
- ·Javascript风格要素..
- ·Javascript风格要素
- ·动态加载JavaScript
- ·JavaScript陷阱
- ·ajax技巧
- ·IE对CSS样式表的限制..
- ·什么是Javascript匿..
随机博文
