会员
周边
新闻
博问
闪存
众包
赞助商
Chat2DB
所有博客
当前博客
我的博客
我的园子
账号设置
会员中心
简洁模式
...
退出登录
注册
登录
淡然
博客园
首页
新随笔
联系
订阅
管理
ajax请求队列
由于同一个页面可能有很多个xmlhttp,所以如果在页面中定义很多个xmlhttp对象的话那样显得很冗余,所以就设计了个请求队列来一个一个的执行请求
ajaxQueue
1
/**/
/*
-------------------
*/
2
/**/
/*
ajax请求模块
*/
3
/**/
/*
-------------------
*/
4
5
/**/
/*
创建一个xmlhttp实例
*/
6
function
createXMLHTTP()
{
7
var
xmlhttp
=
null
; ;
8
try
{
9
xmlhttp
=
new
ActiveXObject(
"
Msxml2.XMLHTTP
"
);
10
}
catch
(e)
{
11
try
{
12
xmlhttp
=
new
ActiveXObject(
"
Microsoft.XMLHTTP
"
);
13
}
catch
(e)
{
14
xmlhttp
=
false
;
15
}
16
}
17
18
if
(
!
xmlhttp
&&
typeof
XMLHttpRequest
!=
'
undefined
'
)
{
19
xmlhttp
=
new
XMLHttpRequest();
20
}
21
Request
=
xmlhttp;
22
}
23
/**/
/*
判断是否登出
*/
24
function
isLoginOut(rep)
25
{
26
if
(rep
==
"
loginout
"
)
{
27
28
document.location.href
=
"
Login.aspx
"
;
29
}
30
}
31
32
var
requestQueue
=
new
Array();
//
请求队列
33
var
Request;
34
var
idQueue
=
new
Array();
35
var
_request, _id;
//
当前请求参数
36
var
finish
=
true
;
//
请求是否结束了
37
38
createXMLHTTP();
39
40
/**/
/*
新异步请求入队列
*/
41
function
newRequest(str,id)
{
42
requestQueue.push(str);
43
idQueue.push(id);
44
}
45
/**/
/*
开始请求
*/
46
function
beginRequest()
47
{
48
finish
=
false
;
49
_request
=
requestQueue.shift();
50
_id
=
idQueue.shift();
51
Request.open(
"
POST
"
,_request,
true
);
52
Request.onreadystatechange
=
_RequestCallBack;
53
Request.send();
54
}
55
/**/
/*
处理请求得到的数据
*/
56
function
_RequestCallBack()
{
57
if
(Request.readystate
==
4
&&
Request.status
==
200
)
{
58
rep
=
Request.responseText;
59
isLoginOut(rep);
60
document.getElementById(_id).innerHTML
=
rep;
61
finish
=
true
;
62
//
判断是否还有请求在队列中
63
if
(requestQueue.length
>
0
)
64
beginRequest();
65
}
66
//
网络等原因中途出错接着处理下一个请求
67
}
posted @
2009-11-05 16:48
淡然
阅读(
704
) 评论(
1
)
收藏
举报
刷新页面
返回顶部
公告