微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

AJAX返回readystate = 1

如何解决AJAX返回readystate = 1

我是新来的。我在用FLASK和python。我从flask方法尝试在AJAX调用中返回html表行。

HTML Page has

<div>
<table id="tableQuestions">

</table>
//AJAX Requests to get
function loadQuestions(){

  var xmlHttp = new XMLHttpRequest();

   xmlHttp.onreadystatechange = function() {
   alert(xmlHttp.readyState + " " + xmlHttp.status);
   if (xmlHttp.readyState==4 && xmlHttp.status == 200) {
       var response = xmlHttp.responseText;
       console.log(response);
       document.querySelector("#tableQuestions").innerHTML=response;
     }
   xmlHttp.open("GET",'/gS',true);
   xmlHttp.send(null);

  }
}

烧瓶路线具有

#test.mix() returns html table rows

@gbp.route('/gS')
def gS():
    test=mn()
    response = make_response(test.mix(quesCount=4),200)
    response.mimetype="text/html"

在Safari调试器中,我可以看到responseText包含来自服务器的表数据,但是readystate保持为1,状态为= 0

您能建议我如何解决这个问题?

解决方法

你有

xmlHttp.open("GET",'/gS',true);
xmlHttp.send(null);

在您的xmlHttp.onreadystatechange回调函数中,这可能会导致奇怪的结果。

尝试查看类似(未经测试)的效果是否更好:

//AJAX Requests to get
function loadQuestions() {
  var xmlHttp = new XMLHttpRequest();
  xmlHttp.open("GET",true);
  xmlHttp.onreadystatechange = function() {
    alert(xmlHttp.readyState + " " + xmlHttp.status);
    if (xmlHttp.readyState==4 && xmlHttp.status == 200) {
      var response = xmlHttp.responseText;
      console.log(response);
      document.querySelector("#tableQuestions").innerHTML=response;
    }
  }
  xmlHttp.send(null);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。