【问题标题】:AJAX readyState is not changing to 4 in my codeAJAX readyState 在我的代码中没有更改为 4
【发布时间】:2012-10-30 18:20:45
【问题描述】:

这是我的代码 sn-p,无法理解这是什么问题,因为 xmlhttp.readyState 没有更改为 4。

document.getElementById("opencloseimg").src = "images/minus.jpg";
//The page we are loading.
var serverPage = "calendar.php";
//Set the open close tracker variable.
showCalendar = false;
var obj = document.getElementById(objID);
xmlhttp.open("GET", serverPage,true);
xmlhttp.onreadystatechange = function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        obj.innerHTML = xmlhttp.responseText;
    }
}
xmlhttp.send(null);

【问题讨论】:

  • 你在哪里定义xmlhttp
  • 现在有很多不错的 jQuery 库可供使用。使用它们并避免这种笨拙的xmlhttp 东西。

标签: php javascript xml ajax


【解决方案1】:

可能是因为您获取的 URL 不可用?

您在 JavaScript 控制台中看到任何错误吗?

在 statechange 函数的开头放置一个消息显示:

xmlhttp.onreadystatechange = function() {
    console.log ('xmlhttp : ' + xmlhttp.readyState + ', ' + xmlhttp.status);
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
      obj.innerHTML = xmlhttp.responseText;
    }

查看显示的内容。

【讨论】:

    【解决方案2】:

    可能响应状态码不是200,也可能是304(未修改)等

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-18
      • 1970-01-01
      相关资源
      最近更新 更多