firefox下XMLHttpRequest重用时nsIXMLHttpRequest.send发生错误的解决方案

Go 旅城通票 2009-07-07 03:50:53
加精
  最近在写一个ajax无刷新聊天的程序,在调试程序时,发现firefox下出现了一个严重错误~~,如下
Error: uncaught exception: [Exception... "Component returned failure code: 0xc1f30001
(NS_ERROR_NOT_INITIALIZED) [nsIXMLHttpRequest.send]" nsresult: "0xc1f30001 (NS_ERROR_NOT_INITIALIZED)"
location: "JS frame :: [URL censored] :: zendGegevens :: line 68" data: no]


  上网找了很多资料,有的说是ajax重用的为问题【确实重用了ajax对象,使用的是一个数组存储ajax对象,以便重用】,但是一直没有找到解决方案。。。。

  我在聊天里面启动了两个计时器用来获取聊天信息和在线用户列表,时间间隔都是一样的,这样有可能两个计时器同时进入获取xhr对象的代码块中,导致同一个对象被使用。【js没有加锁的代码,如lock和unlock】,在firefox下就出现了上诉的问题了,ie下是没有问题的。

  今天又去google了下,发现了一篇e文,出现的错误和我的出现的一样,于是照着葫芦画瓢,更正以后没在出现上述的问题了,哈哈哈哈~~~happy。

  查看e文点击这里

  If you take an xmlhttp object that's busy sending and receiving and tell it to send another request, it simply stops doing whatever it does and sends out the new request,Except in Mozilla。

  上面大概意思是当一个xhr对象仍然发送和接受数据的时候,你再使用此对象发出请求,此xhr对象停止任何动作不管它正在干什么或者发送新请求。然后在Mozilla浏览器里面就出现上面的错误了。【e文不太行,不知道是不是这个意思??!~~~~~】

  解决方案

if (isBusy)
{
xmlhttp.onreadystatechange = function () {}
xmlhttp.abort();
}

Don't ask me why this is necessary, but it works.

下面这句就是不要问我为什么这样做,但是这个确实起作用了。


最后发更改后的ajax应用程序池,下载点这里

String.prototype.trim=function(){return this.replace(/$\s*|\s*$/g,'');}
var Showbo={author:'showbo',homepage:'http://www.coding123.net'};
//获取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;itry{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,err:false};
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);
}
}




哈哈~~~~~~~放分。。。

本文来自:http://www.coding123.net/blogdetail429.html
...全文
2378 36 打赏 收藏 转发到动态 举报
写回复
用AI写文章
36 条回复
切换为时间正序
请发表友善的回复…
发表回复
Jimbo 2011-01-31
  • 打赏
  • 举报
回复
好哎,理解。
flydycfly 2010-03-24
  • 打赏
  • 举报
回复
不错!!!!
Gillber 2010-03-01
  • 打赏
  • 举报
回复
谢谢分享。。。。学习了
Dark__Angel 2010-02-11
  • 打赏
  • 举报
回复
顶!!!!!!!!!!!!!1

好贴
Fuller 2010-01-05
  • 打赏
  • 举报
回复
向楼主请教:

为什么要维护一个XHLHttpRequest对象池?同每次使用都new一个相比有什么优势?

我感觉XMLHttpRequest每次发送的时候怎样使用TCP连接我们又控制不了,重用XMLHttpRequest并不能保证重用TCP连接,TCP连接是否保持受很多因素影响,例如:keep-alive, pipeline。如果每次new一个,而且浏览器有空余的TCP的话,还可以并行建立多个连接,是否更好?

最近一直在研究AJAX平台,看到这些不同设计,不知道哪个好
saisky 2009-07-16
  • 打赏
  • 举报
回复
jf~~
mykelly6 2009-07-15
  • 打赏
  • 举报
回复
接分。
lwj7891875 2009-07-10
  • 打赏
  • 举报
回复
fztjava 2009-07-10
  • 打赏
  • 举报
回复
谢谢分享
william3033 2009-07-09
  • 打赏
  • 举报
回复
顶,接分。。
shore1111 2009-07-09
  • 打赏
  • 举报
回复
学习了
阿非 2009-07-09
  • 打赏
  • 举报
回复
学习一下~
qiubo1818 2009-07-09
  • 打赏
  • 举报
回复
哥哥,太好了啊
ImAHunter 2009-07-09
  • 打赏
  • 举报
回复
复杂的ajax……没写过这么复杂的,呵呵。
xuksdxx 2009-07-09
  • 打赏
  • 举报
回复
值得一学习
andrew_hxp 2009-07-09
  • 打赏
  • 举报
回复
太复杂了
街头小贩 2009-07-09
  • 打赏
  • 举报
回复
么也不说了!jf
antony1029 2009-07-09
  • 打赏
  • 举报
回复
不错。呵呵
shagoo 2009-07-08
  • 打赏
  • 举报
回复
顶LZ~ 辛苦了~
dhgdmw 2009-07-08
  • 打赏
  • 举报
回复
好,收藏了
加载更多回复(15)

52,797

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧