Ajax多线程
(收藏)http://civ3.cnblogs.com/archive/2006/04/13/374584.html 来源
尝试MagicAjax,发现非常方便,但是不支持多线程异步通讯。
为此我又根据需要自己写了一个小Ajax,作为MagicAjax的补充。ie和ff上运行良好。
这其实是从去年的Flexible里面改的。
请求包括命令cmd和参数par
这个Ajax对象封装了一个成员xmlhttp
两个方法:
请求req和响应rsp
//
满足此处需要就行了,以后再根据不同的需要扩展。
尝试MagicAjax,发现非常方便,但是不支持多线程异步通讯。
为此我又根据需要自己写了一个小Ajax,作为MagicAjax的补充。ie和ff上运行良好。
这其实是从去年的Flexible里面改的。
1
//
2
if(window.ActiveXObject)STD=false;
3
else STD=true;
4
//
5
function Ajax(fun){
6
this.a=(STD)?(new XMLHttpRequest):(new ActiveXObject('Microsoft.XMLHTTP'));
7
var ajax=this;
8
var xmlhttp=this.a;
9
10
xmlhttp.onreadystatechange=function(){
11
if(xmlhttp.readyState==4)
12
fun(ajax.rsp());
13
}
14
15
this.req=function(cmd,par){
16
xmlhttp.open("POST","MyAjax.asmx/"+cmd,true);
17
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
18
xmlhttp.send("par="+par);
19
}
20
this.rsp=function(){
21
return (STD)?xmlhttp.responseXML.firstChild.textContent:xmlhttp.responseXML.text;
22
}
23
}
24
其中fun是回调函数,用来处理返回消息。
//2
if(window.ActiveXObject)STD=false;3
else STD=true;4
//5
function Ajax(fun){6
this.a=(STD)?(new XMLHttpRequest):(new ActiveXObject('Microsoft.XMLHTTP'));7
var ajax=this;8
var xmlhttp=this.a;9
10
xmlhttp.onreadystatechange=function(){11
if(xmlhttp.readyState==4)12
fun(ajax.rsp());13
}14
15
this.req=function(cmd,par){16
xmlhttp.open("POST","MyAjax.asmx/"+cmd,true);17
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");18
xmlhttp.send("par="+par);19
}20
this.rsp=function(){21
return (STD)?xmlhttp.responseXML.firstChild.textContent:xmlhttp.responseXML.text;22
}23
}24

请求包括命令cmd和参数par
这个Ajax对象封装了一个成员xmlhttp
两个方法:
请求req和响应rsp
//
满足此处需要就行了,以后再根据不同的需要扩展。



xmlhttp.onreadystatechange
浙公网安备 33010602011771号