我正在使用Ajax呼叫开发,在调试时,我发现Ajax发送了请求/响应的时间比我实际想象的很多。

很久以前,我得到了很好的文件,描述了现场发生的事情,但我丢失了。

如今,网络上的Ajax教程仅谈论如何编码和这些语句仅检查 readystate == 4status == 200 这不能为像我这样的人提供良好的解释。

我用下面的代码测试了流,我认为很奇怪。我的困惑是为什么Ready 4 State出现了两次?根据定义, 准备好4 方法 完全的 因此,应该没有理由完成两次?

输出

START
ready 1                //loading
START
ready 2                //loaded
ready 2 status=200     //loaded
START
ready 3                //interactive
ready 3 status=200     //interactive
START
ready 4                //complete
START
ready 4                //complete   ... again???

测试代码片段

xmlHttp.onreadystatechange = function() {

                alert("START");

                if(xmlHttp.readyState == 0) {
                    alert('ready 0');
                    alert('ready 0 status=' + xmlHttp.status);
                }

                if(xmlHttp.readyState == 1) {
                    alert('ready 1');
                    alert('ready 1 status=' + xmlHttp.status);
                }

                if(xmlHttp.readyState == 2) {
                    alert('ready 2');
                    alert('ready 2 status=' + xmlHttp.status);
                }

                if(xmlHttp.readyState == 3) {
                    alert('ready 3');
                    alert('ready 3 status=' + xmlHttp.status);
                }

                if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
                    alert('ready 4');
                } 
   }                 
有帮助吗?

解决方案

你可以阅读 quirksmode 关于不同的浏览器对Ajax呼叫和ReadyState的行为方式。

我发现 这个 链接使用 Abort 命令将发布4号准备局(尚未对其进行测试) - 您是否正在使用 Abort?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top