我想让 html 页面上的超链接运行一个在我的 python 文件中定义的变量。该变量将清除我的数据库。这是我要使用的代码。
python
@app.route('/log')
def log():
cleardb = db.session.delete()
return render_template('log.html', cleardb=cleardb)
HTML
<a onclick="myFunction()">Clear database</a>
Javascript
<script>
function myFunction()
</script>
我不知道运行变量需要什么 javascript。我想让 cleardb 被触发,以便它删除数据库。
谢谢
最佳答案
您需要使用 javascript 向/log 发出 ajax 请求,它看起来像这样:
function myFunction() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == XMLHttpRequest.DONE ) {
if (xmlhttp.status == 200) {
//Do Success functionality here
}
else if (xmlhttp.status == 400) {
//Handle 400 errors here
}
else {
//All other errors go here
}
}
};
xmlhttp.open("GET", "/log", true);
xmlhttp.send();
}
关于javascript - 如何使用javascript从html文件触发python中的变量/方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39087614/