手机
当前位置:查字典教程网 >编程开发 >AJAX相关 >AJAXRequest v0.2
AJAXRequest v0.2
摘要:更新:1)更改构造函数,使带参数,简化使用的步骤类名:AJAXRequest创建方法:varajaxobj=newAJAXRequest(m...

更新:

1)更改构造函数,使带参数,简化使用的步骤

类名:AJAXRequest

创建方法:

varajaxobj=newAJAXRequest(method,url,async,content,callback);

如果创建失败则返回false

属性:method-请求方法,字符串,POST或者GET,默认为POST

url-请求URL,字符串,默认为空

async-是否异步,true为异步,false为同步,默认为true

content-请求的内容,如果请求方法为POST需要设定此属性,默认为空

callback-回调函数,即返回响应内容时调用的函数,默认为直接返回,回调函数有一个参数为XMLHttpRequest对象,即定义回调函数时要这样:functionmycallback(xmlobj)

方法:send()-发送请求,无参数

一个例子:

复制代码 代码如下:

<scripttype="text/javascript"src="ajaxrequest.js"></script>

<scripttype="text/javascript">

//请求方式GET,URL为default.asp,异步

varajaxobj=newAJAXRequest("GET","default.asp",true,null,MyCallback);//创建AJAX对象

ajaxobj.send();//发送请求

functionMyCallback(xmlObj){

document.write(xmlobj.responseText);

}

ajaxrequest.js

复制代码 代码如下:

/*------------------------------------------

Author:xujiwei

Website:http://www.xujiwei.cn

E-mail:vipxjw@163.com

Copyright(c)2006,AllRightsReserved

------------------------------------------*/

functionAJAXRequest(pmethod,purl,pasync,pcontent,pcallback){

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;

this.method=pmethod;

this.url=purl;

this.async=pasync;

this.content=pcontent;

this.callback=pcallback;

this.send=function(){

if(!this.method||!this.url||!this.async)returnfalse;

xmlObj.open(this.method,this.url,this.async);

if(this.method=="POST")xmlObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

xmlObj.onreadystatechange=function(){

if(xmlObj.readyState==4){

if(xmlObj.status==200){

ObjSelf.callback(xmlObj);

}

}

}

if(this.method=="POST")xmlObj.send(this.content);

elsexmlObj.send(null);

}

}

【AJAXRequest v0.2】相关文章:

AJAX 随记

AJAX实现仿Google Suggest效果

ajax类AJAXRequest v0.8.01 2008-01-31 最新版附使用帮助

AJAX客户端说明,XMLHttpRequest对象

ajax提交整个from表单示例代码

基于AJAX的分页类实现代码

AJAX XMLHttpRequest对象详解

163 AJAX Tab

建立XMLHttpRequest对象

ajax给全局变量赋值问题解决示例

精品推荐
分类导航