[英]Python flask script not executing as expected
我正在嘗試基於通過 javascript 單擊按鈕來執行 function。我已經復制了有關如何執行此操作的教程,但沒有任何反應。
我在應該執行的 function 中添加了一個斷點 (background_process_test),但程序從不執行 function。我錯過了什么才能讓這個簡單的教程正常工作? 我正在與女服務員一起在本地機器上運行。
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 代碼:
<!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>
更新:網絡選項卡
我以某種方式刪除了“$”符號。 由於視覺工作室中的間距,它給出了一個錯誤。 正確設置間距,問題解決了。 非常感謝您為我指明了正確的方向。
以下代碼中有錯別字:
('a#test').on('click', function(e) {
...
})
具體來說,您嘗試使用 jquery 獲取與“a#test”匹配的元素列表。 代碼應該是:
$('a#test').on('click', function(e) {
...
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.