用法:
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="<tableid=message>"+
"<tr>"+
"<td>"+
"<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();
}
}
}