手机
当前位置:查字典教程网 >编程开发 >AJAX相关 >一个封装的Ajax类
一个封装的Ajax类
摘要:用法:newAjax().Request(url,cmd,async,method,postString,title)参数:url:请求页面...

用法:

newAjax().Request(url,cmd,async,method,postString,title)

参数:

url:请求页面URL(必填)

cmd:返回值处理函数(必填)

async:是否异步,(ture|false),默认true

method:请求方式,(post|get),默认get

postString:请求方式为post时,请求内容

title:请求内容标题

复制代码 代码如下:

//Ajax封装2007-3-13

functioncreateXMLHttpRequest(){

try{

if(window.XMLHTTPRequest){

returnnewXMLHttpRequest();

}

elseif(window.ActiveXObject){

returnnewActiveXObject("Microsoft.XMLHTTP");

}

}

catch(e){alert("XMLHttpRequest对象无法创建!请检查IE安全设置!");}

}

functionmessageDiv(t)

{

varv=document.createElement("<div>");

v.innerHTML="<tablestyle="width:300px;"id=message>"+

"<trstyle="font-size:12px;background-color:#EEEEff;color:#227933;height:20px">"+

"<tdstyle="padding:2px;border-top:1pxsolid#E1E1E1;border-left:1pxsolid#E1E1E1;border-bottom:1pxsolid#818181;border-right:1pxsolid#A1A1A1">"+

"<nobr><imgsrc=refresh.gifalign=absmiddle>"+t+",<spanid=Span1>连接未初始化...</span></nobr></td></tr></table>";

varl=document.getElementsByName("message").length;

v.style.cssText="position:absolute;bottom:"+(l*24)+"px;left:0px;display:none";

document.body.appendChild(v);

this.clear=function(){

document.body.removeChild(v);

varmsg=document.getElementsByName("message");

for(vari=0;i<msg.length;i++){

msg[i].parentNode.style.cssText="position:absolute;bottom:"+(i*24)+"px;left:0px";

}

}

this.showmsg=function(s){

v.style.display="";

v.all.Span1.innerHTML=s;

}

}

functionAjax(){

varx=newcreateXMLHttpRequest();

this.Request=function(url,cmd,async,method,postString,title){

if(method!="post")method="post";elsemethod="get";

if(async!=true)async=true;elseasync=false;

if(typeof(postString)!="string")postString="";

if(typeof(title)!="string")title="正在获取数据";elsetitle="正在获取"+title;

varmsgbox=newmessageDiv(title);

x.onreadystatechange=function()

{

if(async)switch(x.readystate){

case1:

msgbox.showmsg("正在初始化连接...");

return;

case2:

msgbox.showmsg("正在发送数据...");

return;

case3:

msgbox.showmsg("正在接收数据...");

return;

case4:

msgbox.showmsg("数据接收完成...");

if(x.status==200){

cmd(x.responseText);

msgbox.clear();

}

else{

msgbox.showmsg("请求失败,"+x.statustext+"("+x.status+")");

setTimeout(msgbox.clear,3000);

}

return;

}

}

x.open(method,url,async);

if(method=="post"){msgbox.showmsg("正在接收数据...");x.send(postString);}elsex.send();

if(!async){

msgbox.showmsg("数据接收完成...");

cmd(x.responseText);

msgbox.clear();

}

}

}

【一个封装的Ajax类】相关文章:

AJAX封装类使用指南

html+js+php一次原始的Ajax请求示例

一个简单Ajax类库及使用方法实例分析

一个方便AJAX开发的通用类

一个AJAX自动完成功能的js封装源码[支持中文]第1/2页

聊一聊Ajax的优缺点

js与jQuery实现的兼容多浏览器Ajax请求实例

ajax局部刷新一个div下jsp内容的方法

下拉菜单的级联操作(ajax)

Baidu Musicbox 用到的ajax代码

精品推荐
分类导航