doubeiji2602 2015-06-03 19:45
浏览 31

状态永远不会达到4

In the code below I try to post some data to a table and then retrieve several records from the table afterwards. For testing purposes I have only included a dummy query in the php, which returns a valid xml.

But the Javascript readystate only reach readystate 1 (twice). As the php seems to be working fine I suspect I have scripted the javascript incorrectly. Is it a problem with the "request" variable?

$(document).ready(function() {
  $('form').submit(function(event) { //Trigger on form submit
    $('#name + .throw_error').empty(); //Clear the messages first

    var name = $('input[name=name]').val();
    var request = !window.XMLHttpRequest ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest;
    request.onreadystatechange = function() {

      if (request.readyState == 4 && request.status == 200) {
        var xml = request.responseXML;
        var dynamiclist = '';
        document.getElementById("myLink").innerHTML = '';
        var posts = xml.documentElement.getElementsByTagName("post");
        for (var i = 0; i < posts.length; i++) {
          var Msg = posts[i].getAttribute("Msg");
          alert("test");
          var dynamiclist = dynamiclist + '<article class="middleContent"><header><h2><a href="#" title="Post">' + Msg + '</a></h2></header> </article>';
          document.getElementById("myLink").innerHTML = dynamiclist;
        };
      };
    };
    request.open('POST', 'process.php', true);
    request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    request.send('name=' + name);
  });
});

</div>
  • 写回答

2条回答 默认 最新

  • doubu5035 2015-06-03 23:32
    关注

    Using jQuery makes sense here.

    Simple solution however most probably is to simply remove the last "true" in request.open(), which makes the request an asynchronous, non-blocking one (which it really should be anyway). MDN also states at onreadystatechange that you shouldn't be using it with synchronous requests (aka requests that have "true" as third argument for open())

    Source: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起