我刚开始使用python和flask(对于raspberry pi)。我想要一个显示来自mysql的数据的Web应用程序而没有刷新页面我试过这个
app = Flask(__name__)
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = 'raspberry'
app.config['MYSQL_DB'] = 'tesdb'
mysql = MySQL(app)
@app.route('/')
def interactive():
return render_template('interactive.html')
@app.route('/ambildata')
def ambildata():
cur = mysql.connection.cursor()
cur.execute('''SELECT*FROM contoh WHERE id = 1''')
result = cur.fetchall()
return jsonify(result = result)
interactive.html
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type=text/javascript>
$(document).ready(function(){
setInterval(function (data) {
$("#result").text(data.result);
}, 3000);
});
</script>
</head>
<body>
<div class='container'>
<p id=result></p>
</div>
</body>
但我无法看到网页上的数据