我对此还是个新手。我正在使用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);
}
}Flask路由具有
#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

你能建议我如何克服这个问题吗?
发布于 2020-10-20 20:38:44
你有
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);
}https://stackoverflow.com/questions/64445034
复制相似问题