当前位置: 网学 > 编程文档 > Ajax > 正文

ajax问题总结 比较全

来源:Http://myeducs.cn 联系QQ:点击这里给我发消息 作者: 用户投稿 来源: 网络 发布时间: 13/07/11
转换的异同的,有兴趣的参考下
Firefox下的AJAX onreadystatechange问题
最后放一个自己写的ajax类库~~~~~O(∩_∩)O哈哈~~~~~完毕
复制代码 代码如下:
String.prototype.trim=function(){return this.replace(/$\s*|\s*$/g,'''');}
var Showbo={author:''showbo''};
//获取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:[]//存储ajax对象的数组
,getObject:function(){//从数组中获取ajax对象,如果未返回则新建一个ajax对象
for(var i=0;i<this.pools.length;i++)
if(this.pools[i].readyState==0||this.pools[i].readyState==4)return this.pools[i];
this.pools[this.pools.length]=new XMLHttpRequest();
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("未设置配置文件!");
var method=cfg.method,asy="boolean"==typeof(cfg.asy)?cfg.asy:true;
if(!method||method!="post")method="get";
if(method.toLocaleLowerCase()==''get''){
var _dc=new Date().getTime();//加时间戳防止ie浏览器下的缓存
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(!cfg.params)cfg.params='''';
var o=this.getObject();
if(!o)throw("未能创建ajax对象!");
o.open(method,cfg.url,asy);
if(method.toLocaleLowerCase()==''post'')o.setRequestHeader("content-type","application/x-www-form-urlencoded");
o.send(cfg.params);
o.onreadystatechange=function(){
if(o.readyState==4){
if(o.status==200||o.status==0){
if("function"==typeof(cfg.success))cfg.success(o,cfg.otherParams);
}
else if("function"==typeof(cfg.failure))cfg.failure(o,cfg.otherParams);
}
}
}
}

网学推荐

免费论文

原创论文

设为首页 | 加入收藏 | 论文首页 | 论文专题 | 设计下载 | 网学软件 | 论文模板 | 论文资源 | 程序设计 | 关于网学 | 站内搜索 | 网学留言 | 友情链接 | 资料中心
版权所有 QQ:3710167 邮箱:3710167@qq.com 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
Copyright 2008-2015 myeducs.Cn www.myeducs.Cn All Rights Reserved 湘ICP备09003080号