前端通过jinjia2 访问后端python flask中的数据,python访问mysql数据库并返回数值,目前前端可以正常显示数据库中的数值,但是想定时局部刷新显示,但是却不知道应该怎样写,用了几种方法都是直接刷新时直接将html语句显示出来,而不是通过访问后端调用的。请帮忙解决,谢谢。感激不尽。
麻烦帮忙补全代码,谢谢
前端html页面,目前可以正常获取flask中的数据库内容并显示。
<div class="gaojing">
{% for item in data_yujing %}
<table id="gj" width="100%" border="0" cellspacing="4" style="color: #ffffff">
<tr>
<tr>
<td>{{ item.lsdcode }}</td>
<td>{{ item.lsdname }}</td>
<td><div align="justify">{{ item.yujing }}</div></td>
</tr>
</table>
{% endfor %}
</div>
不知道应该怎样写定时局部刷新,我现在写的存在问题,是再div里加载了整个html页面,求指点,代码究竟应该怎样写。
setInterval('shuaxin()', 3000);
function shuaxin() {
$.ajax({
type:"GET",
dataType:"HTML",
url:"/",
success:function (data){
$('.gaojing').html(data);
console.log(data);
}
});
}
后端python flask 代码
@app.route('/')
def lixian():
######获取所有信息#######
# 1.连接MySQL
conn = pymysql.connect(host="127.0.0.1", port=3306, user='root', passwd="Bjcsf123", charset='utf8', db='keshihua')
cursor2 = conn.cursor(cursor=pymysql.cursors.DictCursor)
# 2.提交SQL语句
sql2 = "select * from keshihua.yujing"
cursor2.execute(sql2)
data_yujing = cursor2.fetchall()
# 3.关闭SQL连接
cursor2.close()
conn.close()
print(data_list)
return render_template("index.html",data_yujing=data_yujing)