[英]Python flask script not executing as expected
I'm trying to execute a function based on a button click via javascript. I've copied a tutorial on how to do this, but nothing happens.我正在尝试基于通过 javascript 单击按钮来执行 function。我已经复制了有关如何执行此操作的教程,但没有任何反应。
I've added a break point in the function that should be executed (background_process_test), but the program never executes the function. What am I missing to get this simple tutorial working?我在应该执行的 function 中添加了一个断点 (background_process_test),但程序从不执行 function。我错过了什么才能让这个简单的教程正常工作? I'm running on a local machine with waitress.
我正在与女服务员一起在本地机器上运行。
python code: python 代码:
from flask import Flask, render_template, request
from flask import jsonify
from waitress import serve
app = Flask(__name__)
#rendering the HTML page which has the button
@app.route('/json')
def json():
return render_template('json.html')
#background process happening without any refreshing
@app.route('/background_process_test')
def background_process_test():
print ("Hello")
return ("nothing")
if __name__ == "__main__":
app.debug = True
serve(app, host='0.0.0.0', port=8080) #WAITRESS!
html code: html 代码:
<!DOCTYPE html>
<html lang="en">
<head>
<!--<script src="/jquery/jquery-3.6.0.min.js"></script>-->
<script src="{{url_for('static', filename='jquery-3.6.0.min.js')}}"></script>
<script type=text/javascript>
(function() {
('a#test').on('click', function(e) {
e.preventDefault()
$.getJSON('/background_process_test', function(data) {
//do nothing
})
return false;
})
})
</script>
</head>
<body>
<div class='container'>
<h3>Test</h3>
<form>
<a href=# id=test><button class='btn btn-default'>Test</button></a>
</form>
</div>
</body>
</html>
Update: Network tab更新:网络选项卡
i somehow deleted the '$' sign.我以某种方式删除了“$”符号。 It was giving an error due to spacing in visual studio.
由于视觉工作室中的间距,它给出了一个错误。 Setting the spacing right, solved the issue.
正确设置间距,问题解决了。 Many thanks for pointing me in the right direction.
非常感谢您为我指明了正确的方向。
There is a typo in the following code:以下代码中有错别字:
('a#test').on('click', function(e) {
...
})
Specifically, your attempt to use jquery get the list of elements that match 'a#test'.具体来说,您尝试使用 jquery 获取与“a#test”匹配的元素列表。 The code should be:
代码应该是:
$('a#test').on('click', function(e) {
...
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.