需求:
flask中使用ajax 处理前端请求,每隔一段时间请求一次,并展示在页面
使用 setInterval(function(){},1000)方法
结果展示:
html:(test.html)
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta charset="UTF-8">
5 <title>2221</title>
6 </head>
7
8 <body>
9
10
11 {#根据实际路径填写#}
12 <script type="text/javascript" src="static/js/jquery.min.js"></script>
13
14 <script type="text/javascript">
15 setInterval(
16 function(){
17 $.ajax({
18 type: "POST",
19 dataType: "json",
20 url: "/test112",//后端请求url地址
21 data: '11',//后端请求参数
22 success: function (result) {
23 console.log(result);
24 {
25 alert(result)
26 }
27 },
28 error: function (result) {
29 console.log(result);
30 {
31 alert(result);
32 }
33 }
34 })
35 }
36
37 , 5000); // 5秒后再次调用
38 </script>
39 </body>
40 </html>
后端flask:
1 @app.route('/test',methods=['GET','POST'])
2 def test():
3 return render_template("test/test.html")
4
5
6 @app.route('/test112',methods=['GET','POST'])
7 def test112():
8 res = 11111
9 return