flask中使用ajax 处理前端请求,每隔一段时间请求一次

需求:

flask中使用ajax 处理前端请求,每隔一段时间请求一次,并展示在页面

使用 setInterval(function(){},1000)方法

结果展示:

flask中使用ajax 处理前端请求,每隔一段时间请求一次

html:(test.html)

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>2221</title>
</head> <body> {#根据实际路径填写#}
<script type="text/javascript" src="static/js/jquery.min.js"></script> <script type="text/javascript">
setInterval(
function(){
$.ajax({
type: "POST",
dataType: "json",
url: "/test112",//后端请求url地址
data: '11',//后端请求参数
success: function (result) {
console.log(result);
{
alert(result)
}
},
error: function (result) {
console.log(result);
{
alert(result);
}
}
})
} , 5000); // 5秒后再次调用
</script>
</body>
</html>
后端flask:
 @app.route('/test',methods=['GET','POST'])
def test():
return render_template("test/test.html") @app.route('/test112',methods=['GET','POST'])
def test112():
res = 11111
return jsonify(res)

上一篇:flask中使用ajax 处理前端请求,每隔一段时间请求不通的接口,结果展示同一页面


下一篇:js之展开收缩菜单,用到window.onload ,onclick,