首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AJAX返回readystate=1

AJAX返回readystate=1
EN

Stack Overflow用户
提问于 2020-10-20 20:19:47
回答 1查看 54关注 0票数 0

我对此还是个新手。我正在使用FLASK和python。在flask方法中,我尝试在AJAX调用中返回一个html表行。

代码语言:javascript
复制
HTML Page has

<div>
<table id="tableQuestions">

</table>
代码语言:javascript
复制
//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);

  }
}

Flask路由具有

代码语言:javascript
复制
#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,status =0

你能建议我如何克服这个问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-20 20:38:44

你有

代码语言:javascript
复制
xmlHttp.open("GET", '/gS', true);
xmlHttp.send(null);

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

试着看看这样的东西(未测试)是否表现得更好:

代码语言:javascript
复制
//AJAX Requests to get
function loadQuestions() {
  var xmlHttp = new XMLHttpRequest();
  xmlHttp.open("GET", '/gS', 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);
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64445034

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档