INVALID_STATE_ERR:DOM 异常 11

发布于 2024-11-25 17:09:39 字数 1761 浏览 0 评论 0原文

好的,我已经创建了一个有效的 javascript ajax 文件,但它生成了数量惊人的 dom 异常。我不确定为什么会这样,因为从我所看到的来看,我调用的所有元素目前仍然存在。 代码在这里:

window.onload = function(){init();}

function init() {
    ajax = ajaxInit();
    setInterval(function(){ajaxContact(ajax);},2000);
    ajaxContact(ajax);
    ajax.onreadystatechange = function() {update(ajax);}
}

function ajaxInit() {
    if (window.XMLHttpRequest) {
      ajax = new XMLHttpRequest();
    }
    else {
        if (window.ActiveXObject) {
              ajax = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    if (ajax) {
        document.getElementById("status").innerHTML = "AJAX initialized";
        return ajax;
    }
    else {
        docuement.getElementById("status").innerHTML = "Error: AJAX not available";
        return false;
    }

}

function ajaxContact(ajax) {
    try {
        ajax.open("GET","updateAjax.php?" + "ran=" + Math.random(),true);
        ajax.send();
    }
    catch (err) {
        alert(err.message);
        document.getElementById("status").innerHTML = "Error contacting server";
        document.getElementById("loading").src = "images/redx.png";
    }
}

function update(ajax) {
      if (ajax.readyState==4 && ajax.status==200){
          dataObj = eval('(' + ajax.responseText + ')');
          document.getElementById("status").innerHTML = dataObj.status;
          document.getElementById("frameNumber").innerHTML =
          "Frame:" + dataObj.firstFrame + "/" + dataObj.lastFrame;
          document.getElementById("thumbnail").src = dataObj.imgSrc;
      }
      if (ajax.status==404) {
          document.getElementById("status").innerHTML = "Ajax updater not found";
          document.getElementById("loading").src = "images/redx.png";
      }
}

Ok, I've created a working javascript ajax file, but it generates an absurd number of these dom exceptions. I'm not sure why that is, because from what I can see, all the elements I call are currently still in existance.
The code is here:

window.onload = function(){init();}

function init() {
    ajax = ajaxInit();
    setInterval(function(){ajaxContact(ajax);},2000);
    ajaxContact(ajax);
    ajax.onreadystatechange = function() {update(ajax);}
}

function ajaxInit() {
    if (window.XMLHttpRequest) {
      ajax = new XMLHttpRequest();
    }
    else {
        if (window.ActiveXObject) {
              ajax = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    if (ajax) {
        document.getElementById("status").innerHTML = "AJAX initialized";
        return ajax;
    }
    else {
        docuement.getElementById("status").innerHTML = "Error: AJAX not available";
        return false;
    }

}

function ajaxContact(ajax) {
    try {
        ajax.open("GET","updateAjax.php?" + "ran=" + Math.random(),true);
        ajax.send();
    }
    catch (err) {
        alert(err.message);
        document.getElementById("status").innerHTML = "Error contacting server";
        document.getElementById("loading").src = "images/redx.png";
    }
}

function update(ajax) {
      if (ajax.readyState==4 && ajax.status==200){
          dataObj = eval('(' + ajax.responseText + ')');
          document.getElementById("status").innerHTML = dataObj.status;
          document.getElementById("frameNumber").innerHTML =
          "Frame:" + dataObj.firstFrame + "/" + dataObj.lastFrame;
          document.getElementById("thumbnail").src = dataObj.imgSrc;
      }
      if (ajax.status==404) {
          document.getElementById("status").innerHTML = "Ajax updater not found";
          document.getElementById("loading").src = "images/redx.png";
      }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱桃奶球 2024-12-02 17:09:39

<罢工>
您可能尝试在 ajax 上调用 open 和 send,但如果请求在 setInterval 每次调用之间的两秒内未完成,则会抛出错误。您需要在每次调用中检查ajax对象是否已发送或准备好打开(检查ajax.readyState)。

在 Chrome 中,该行

if (ajax.status==404) {

通过访问 ajax.readyStateHEADERS_RECEIVED (2)、LOADING (3 ),完成 (4)。 在访问状态之前尝试确保

if (ajax.readyState == 4 && ajax.status==404) {

对象已准备好。


You are probably trying to call open and send on ajax, but it throws errors if the request has not finished within the two seconds between each call by setInterval. You need to check in each call whether the ajax object has been sent already or is ready for opening (check ajax.readyState).

In Chrome, the line

if (ajax.status==404) {

causes the error by accessing the status before ajax.readyState is HEADERS_RECEIVED (2), LOADING (3), DONE (4). Try making it

if (ajax.readyState == 4 && ajax.status==404) {

to make sure that the object is ready before accessing the status.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文