亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

AJAX 正在返回 readystate=1

AJAX 正在返回 readystate=1

慕萊塢森 2023-05-25 15:43:57
我正在使用 Flask 和 Python。從 Flask 方法中,我試圖在 AJAX 調用中返回 HTML 表格行。HTML 頁面有:<div><table id="tableQuestions"></table>//AJAX Requests to getfunction 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 路線有:#test.mix() returns html table [email protected]('/gS')def gS():    test=mn()    response = make_response(test.mix(quesCount=4),200)    response.mimetype="text/html"在 Safari 調試器中,我可以看到有responseText來自服務器的表數據,但readystate仍然是 1 和status= 0。你能建議我如何克服這個問題嗎?
查看完整描述

1 回答

?
長風秋雁

TA貢獻1757條經驗 獲得超7個贊

你有


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

xmlHttp.send(null);

在你的xmlHttp.onreadystatechange回調函數中,這會導致奇怪的效果。


以下代碼(未經測試)應該表現得更好:


//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);

}


查看完整回答
反對 回復 2023-05-25
  • 1 回答
  • 0 關注
  • 220 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號