欢迎来到福编程网,本站提供各种互联网专业知识!

一个方便AJAX开发的通用类

发布时间:2006-12-23 作者: 来源:转载
Name:AJAXRequestAuthor:HotHeart(xujiwei)Site:http://www.xujiwei.cn/Blog:http://www.xujiwei.cn/blog/Copyright(c)2006,AllRightsReserved类名:AJAXRequest版本:0.3日期:2006-12-18介绍:AJAXRequest是一个方便AJAX开发的通用类,可以方便地进行一些AJ
Name:AJAXRequest
Author:HotHeart(xujiwei)
Site:http://www.xujiwei.cn/
Blog:http://www.xujiwei.cn/blog/
Copyright(c)2006,AllRightsReserved

类名:AJAXRequest
版本:0.3
日期:2006-12-18
介绍:AJAXRequest是一个方便AJAX开发的通用类,可以方便地进行一些AJAX中需要的操作,从而简化开发步骤,减少重复代码编写量。

创建方法:
varajaxobj=newAJAXRequest([url],[callback],[content],[method],[async]);
如果创建失败则返回false

属性:
url-请求URL,字符串,默认为空
callback-回调函数,即返回响应内容时调用的函数,默认为直接返回,回调函数有一个参数为XMLHttpRequest对象,即定义回调函数时要这样:functionmycallback(xmlobj)
content-请求的内容,如果请求方法为POST需要设定此属性,默认为空字符串
method-请求方法,字符串,POST或者GET,默认为POST
async-是否异步,true为异步,false为同步,默认为true

方法
functionsend([url],[callback],[content],[method],[async])
发送请求,可选参数列表为空就使用对象属性

functionget([url],[callback])
使用GET方法请求一个URL,可选参数默认使用对象属性

functionpost(form_obj,[callback],[url],[method])
发送一个表单到指定URL,form_obj为指定表单对象,可选参数为空时使用对象属性

示例:
1.get方法
functiontest1(){
varajax=newAJAXRequest;
ajax.get(
"test.asp",
function(obj){
document.getElementById("test1").value=obj.responseText;
}
);
}
2.post方法
functiontest2(){
varajax=newAJAXRequest;
ajax.post(
document.getElementById("test2c"),
function(obj){
document.getElementById("test2r").innerHTML=obj.responseText;
}
);
}
复制代码 代码如下:
/*------------------------------------------
Author:xujiwei
Website:http://www.xujiwei.cn
E-mail:vipxjw@163.com
Copyright(c)2006,AllRightsReserved
------------------------------------------*/
functionAJAXRequest(){
varxmlObj=false;
varCBfunc,ObjSelf;
ObjSelf=this;
try{xmlObj=newXMLHttpRequest;}
catch(e){
try{xmlObj=newActiveXObject("MSXML2.XMLHTTP");}
catch(e2){
try{xmlObj=newActiveXObject("Microsoft.XMLHTTP");}
catch(e3){xmlObj=false;}
}
}
if(!xmlObj)returnfalse;
if(arguments[0])this.url=arguments[0];elsethis.url="";
if(arguments[1])this.callback=arguments[1];elsethis.callback=function(obj){return};
if(arguments[2])this.content=arguments[2];elsethis.content="";
if(arguments[3])this.method=arguments[3];elsethis.method="POST";
if(arguments[4])this.async=arguments[4];elsethis.async=true;
this.send=function(){
varpurl,pcbf,pc,pm,pa;
if(arguments[0])purl=arguments[0];elsepurl=this.url;
if(arguments[1])pc=arguments[1];elsepc=this.content;
if(arguments[2])pcbf=arguments[2];elsepcbf=this.callback;
if(arguments[3])pm=arguments[3];elsepm=this.method;
if(arguments[4])pa=arguments[4];elsepa=this.async;
if(!pm||!purl||!pa)returnfalse;
xmlObj.open(pm,purl,pa);
if(pm=="POST")xmlObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlObj.onreadystatechange=function(){
if(xmlObj.readyState==4){
if(xmlObj.status==200){
pcbf(xmlObj);
}
else{
pcbf(null);
}
}
}
if(pm=="POST")
xmlObj.send(pc);
else
xmlObj.send("");
}
this.get=function(){
varpurl,pcbf;
if(arguments[0])purl=arguments[0];elsepurl=this.url;
if(arguments[1])pcbf=arguments[1];elsepcbf=this.callback;
if(!purl&&!pcbf)returnfalse;
this.send(purl,"",pcbf,"GET",true);
}
this.post=function(){
varfo,pcbf,purl,pc,pm;
if(arguments[0])fo=arguments[0];elsereturnfalse;
if(arguments[1])pcbf=arguments[1];elsepcbf=this.callback;
if(arguments[2])
purl=arguments[2];
elseif(fo.action)
purl=fo.action;
else
purl=this.url;
if(arguments[3])
pm=arguments[3];
elseif(fo.method)
pm=fo.method.toLowerCase();
else
pm="post";
if(!pcbf&&!purl)returnfalse;
pc=this.formToStr(fo);
if(!pc)returnfalse;
if(pm){
if(pm=="post")
this.send(purl,pc,pcbf,"POST",true);
else
if(purl.indexOf("?")>0)
this.send(purl+"&"+pc,"",pcbf,"GET",true);
else
this.send(purl+"?"+pc,"",pcbf,"GET",true);
}
else
this.send(purl,pc,pcbf,"POST",true);
}
//formToStr
//fromSurfChen
//@urlhttp://www.surfchen.org/
//@licensehttp://www.gnu.org/licenses/gpl.htmlGPL
//modifiedbyxujiwei
//@urlhttp://www.xujiwei.cn/
this.formToStr=function(fc){
vari,query_string="",and="";
for(i=0;ie=fc[i];
if(e.name!=''){
if(e.type=='select-one'){
element_value=e.options[e.selectedIndex].value;
}
elseif(e.type=='checkbox'||e.type=='radio'){
if(e.checked==false){
continue;
}
element_value=e.value;
}
else{
element_value=e.value;
}
element_value=encodeURIComponent(element_value);
query_string+=and+e.name+'='+element_value;
and="&";
}
}
returnquery_string;
}
}

相关推荐