ajax可能会向后台发送多个请求,因为不知道它们个别都什么时候完成,所以不知道是不是页面确实数据已经加载完毕,想知道最后一个请求时什么时候完成的。现在我用了代理去替换httpxmlrequest这个对象,并将这段代码注入到网页里,但问题是不知道页面什么时候数据加载完成

解决方案 »

  1.   

    ++
    xmlHttpRequest.readystate==4 表示完成
      

  2.   

    function ajaxInfo1() {
        this.xmlHttpRequest = null;   //保存XMLHttpRequest请求对象    var objXMLHttp;
        if (window.XMLHttpRequest) {
            objXMLHttp = new XMLHttpRequest();
            this.xmlHttpRequest = createXMLHttpRequestAgent(objXMLHttp);
        }
    }//构造过程拦截
    function createXMLHttpRequestAgent(ao) {
        document.write("  构造过程拦截start  ");
        var count=0;
        var agent = new Object;
        agent.xmlHttpRequest = ao; //被包裹的内核,是真正的通信对象
        agent.syncAttribute = function () { //syncAttribute是用来同步属性的
            try {
                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;
            } catch (e) { }
        };
        agent.trigStateChange = function () { //模拟onreadystatechange
            agent.syncAttribute();
            switch (this.readyState) {
                case 0:
                    document.write("未初始化!  ");
                    break;
                case 1:
                    count++;
                    document.write("开始向服务器发送请求,载入!  ");
                    break;
                case 2:
                    document.write("载入完成!  ");
                    break;
                case 3:
                    document.write("交互,正在解析响应内容!  ");
                    break;
                case 4:
                    if (this.status == 200) {
                        count--;
                        if (count == 0) { alert("all complete!"); }
                        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;
        
    }代理是怎么写的,计数器写在哪合适呢?我的写法合适吗
      

  3.   

    if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
    这样就都完成了、、、