这是我第一次使用Ajax,我使用无限循环ajax的请求从python函数中获取数据。我想在html表中显示数据,但当ajax循环时,屏幕正在加载,没有任何显示。这是我的ajax代码。
<td height="50%"style="border-width:5px; border-color:black; border-style: solid">
<p id = "demo">hihi</p>
<script>
while(True){
$(document).ready(function calllog(){
$.ajax({
type : 'GET',
url : '/for_log',
dataType : 'text',
error : function(){
alert("failed");
},
success : function(data){
document.getElementById("demo").append(data);
}
}
});
}
});
在不继续加载屏幕的情况下,如何在接收和写入数据时打印屏幕?
发布于 2020-06-25 01:22:01
您应该避免使用while(true)
。最好使用setInterval
setInterval(()=>{
$(document).ready(function calllog(){
$.ajax({
type : 'GET',
url : '/for_log',
dataType : 'text',
error : function(){
alert("failed");
},
success : function(data){
document.getElementById("demo").append(data);
}
}
});
}
} , 1000); // every second
这是解决这个问题的另一种方法recursive ajax
https://stackoverflow.com/questions/62560610
复制相似问题