gpt4 book ai didi

javascript - Python Flask 每 60 秒刷新一次表

转载 作者:行者123 更新时间:2023-12-01 03:13:07 25 4
gpt4 key购买 nike

我在下面发布了我的 flask 代码。我想每 60 秒刷新一次表数据Setinterval 函数包含在 html 中。但它并不令人耳目一新。我没有弄清楚 ajax/Javascript 中的确切问题。请有人帮忙解决这个问题。

App.py:

app = Flask(__name__)
app.config['SECRET_KEY'] = 'dsfd'
mysql = MySQL()
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = 'pwd'
app.config['MYSQL_DATABASE_DB'] = 'db'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
mysql.init_app(app)
eachcur = ["A","B","C","D"]

@app.route('/', methods= ['GET'])
def display_deals():


conn = mysql.connect()
c = conn.cursor()
data = ((),)
for curren in eachcur:
query = "SELECT * from "+curren+" ORDER BY Currid DESC LIMIT 1"
c.execute(query)
data = data + c.fetchall()

conn.close()


return render_template("dashboard.html", data=data)

if __name__ == '__main__':
app.run(debug=True)

dashboard.html:

<!doctype html>
<html>
<body>
<script>
setInterval(function() {
$.ajax({
type: "POST",
url: "/",
})
.done(function( data ) {
$("#table-box").val(data)
});
}, 1000 * 60);
</script>
<table border="2" cellpadding="4" cellspacing="5" id="table-box" >
{% for row in data %}
<tr>
{% for d in row %}
<td>{{ d }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
</body>
</html>

控制台输出:

jquery.min.js:4 XHR finished loading: POST "http://localhost:5000/".
send @ jquery.min.js:4
ajax @ jquery.min.js:4
(anonymous) @ (index):7
setInterval (async)
(anonymous) @ (index):6
17:21:45.516 jquery.min.js:4 [Violation] 'load' handler took 334ms

Console snapshot

Console snapshot - 2

最佳答案

您的代码有一些问题:

  1. 您在应该使用 .html() 的地方使用了 .val()

    .val() 用于设置输入字段等内容的值,而不是覆盖整个 DOM 对象。您应该在此处使用 .html()

  2. 您正在将非 html 数据分配给 #table-box

    生成表的 for 语句仅在页面加载时运行。您需要在 AJAX .done() 处理程序中重新生成所有表行。

请参阅下面的示例,该示例解决了这两个问题。

<!doctype html>
<html>
<body>
<script>
setInterval(function() {
$.ajax({
type: "POST",
url: "/",
})
.done(function( data ) {
console.log(data);
var tableHtml = '';
for (row in data) {
tableHtml += '<tr>';
for (d in row) {
tableHtml += '<td>' + d + '</td>';
}
tableHtml += '</tr>';
}
$("#table-box").html(tableHtml)
}).fail(function(jqXHR, textStatus, errorThrown) {
console.log(jqXHR, textStatus, errorThrown);
});
}, 1000 * 60);
</script>
<table border="2" cellpadding="4" cellspacing="5" id="table-box" >
{% for row in data %}
<tr>
{% for d in row %}
<td>{{ d }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
</body>
</html>
<小时/>

编辑:您似乎没有设置路由/ Controller 来处理对 POST / 的 AJAX 请求。你应该尝试这样的事情:

@app.route('/json', methods= ['GET'])     
def display_deals_json():
conn = mysql.connect()
c = conn.cursor()
data = ((),)
for curren in eachcur:
query = "SELECT * from "+curren+" ORDER BY Currid DESC LIMIT 1"
c.execute(query)
data = data + c.fetchall()
conn.close()
return json.dumps(data)

您需要将 AJAX 命令更新为:

$.ajax({
type: "GET",
url: "/json",
})

关于javascript - Python Flask 每 60 秒刷新一次表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45720832/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com