【问题标题】:Code to update website asynchronously not working? Detailed description异步更新网站的代码不起作用?详细说明
【发布时间】:2020-05-13 16:34:37
【问题描述】:

下面是两个调用python文件来更新html的Javascript函数。顶部的工作正常,但底部的工作不正常。没有抛出错误。

function button(key) {
  var xhttp = new XMLHttpRequest();
  xhttp.open("GET", "example.py?keyword="+key, false);
  xhttp.send();
  var response = xhttp.responseText;
  var array = response.split('|');
  document.getElementById("demo").innerHTML = array[0];
  document.getElementById("demo2").innerHTML = array[1];
}
function current(id) {
  var xhttp2 = new XMLHttpRequest();
  xhttp2.open("GET", "example.py?infokey="+id, false);
  xhttp2.send();
  var response = xhttp2.responseText;
  document.getElementById("demo3").innerHTML = response;
}

我已经广泛调试了python代码,这不是问题。有什么想法吗?

【问题讨论】:

    标签: javascript python html css ajax


    【解决方案1】:

    第二个功能不起作用,因为您试图在响应来自服务器之前读取响应。

    试试这个:-

    function current(id) {
      var xhttp2 = new XMLHttpRequest();
      xhttp2.onreadystatechange = function () {
        if (this.readyState === 4 && this.status === 200) {
          var response = xhttp2.responseText;
          document.getElementById("demo3").innerHTML = response;
      }
      xhttp2.open("GET", "example.py?infokey="+id, false);
      xhttp2.send();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-25
      • 2022-12-31
      • 2016-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多