慕萊塢森
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);
}
添加回答
舉報
0/150
提交
取消