想知道什么时候ajax请求全部完成

ajaxzhang 2012-05-08 10:10:54
ajax可能会向后台发送多个请求,因为不知道它们个别都什么时候完成,所以不知道是不是页面确实数据已经加载完毕,想知道最后一个请求时什么时候完成的。现在我用了代理去替换httpxmlrequest这个对象,并将这段代码注入到网页里,但问题是不知道页面什么时候数据加载完成,现在是httpxmlrequest对象代理,但只能知道一次完成。
function ajaxInfo1() {
this.xmlHttpRequest = null; //保存XMLHttpRequest请求对象

var objXMLHttp;
if (window.XMLHttpRequest) {
objXMLHttp = new XMLHttpRequest();
this.xmlHttpRequest = createXMLHttpRequestAgent(objXMLHttp);
}
}

//构造过程拦截
function createXMLHttpRequestAgent(ao) {
document.write("构造过程拦截123start ");
document.write("123 ");
agent = new Object;
document.write("1 agent ");
agent.xmlHttpRequest = ao; //被包裹的内核,是真正的通信对象
agent.syncAttribute = function () { //syncAttribute是用来同步属性的
try {
document.write("同步开始 ");
this.readyState = this.xmlHttpRequest.readyState;
this.responseText = this.xmlHttpRequest.responseText;
this.responseXML = this.xmlHttpRequest.responseXML;
this.status = this.xmlHttpRequest.status; //服务器状态
this.statusText = this.xmlHttpRequest.statusText; //http状态的对应文本
//this.onreadystatechange = this.xmlHttpRequest.onreadystatechange;
document.write("同步结束 ");
} catch (e) { }
};
agent.trigStateChange = function () { //模拟onreadystatechange
agent.syncAttribute();
switch (this.readyState) {
case 0:
document.write("未初始化! ");
break;
case 1:
document.write("开始向服务器发送请求,载入! ");
break;
case 2:
document.write("载入完成! ");
break;
case 3:
document.write("交互,正在解析响应内容! ");
break;
case 4:
if (this.status == 200) {
document.write("与服务器交互完成调用! ");
}
break;
default:
alert("出界");
break;
}
if (agent.onreadystatechange != null) {
agent.onreadystatechange();
}
};
agent.xmlHttpRequest.onreadystatechange = agent.trigStateChange;
agent.abort = function () { //模拟abort
this.xmlHttpRequest.abort();
this.syncAttribute();
};
agent.getAllResponseHeaders = function () { //模拟内核对应的方法
var result = this.xmlHttpRequest.getAllResponseHeaders();
this.syncAttribute();
return result;
};
agent.getResponseHeader = function (headerLabel) { //模拟内核对应的方法
var result = this.xmlHttpRequest.getResponseHeader(headerLabel);
this.syncAttribute();
return result;
};
agent.open = function (method, url, asyncFlag, userName, password) {

this.xmlHttpRequest.open(method, url, asyncFlag, userName, password);
this.syncAttribute();
};
agent.send = function (content) { //模拟内核对应的方法
this.xmlHttpRequest.send(content);
this.syncAttribute();
};
agent.setRequestHeader = function (label, value) { //模拟内核对应的方法
this.xmlHttpRequest.setRequestHeader(label, value);
this.syncAttribute();
};
document.write("构造过程拦截end ");
return agent;

}
...全文
144 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
tongjun741 2012-05-15
  • 打赏
  • 举报
回复
所有的请求发送前记录到队列中,所有请求完成时移除队列中相应的元素,如果队列为空就表示所有请求都完成了

52,797

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧