我在Flask中有3个app.routes,就像那样:
@app.route('/page')
def page_with_buttons():
return render_template('page_with_buttons.html')
@app.route('/api/run_function')
def run_function():
# some function, which long execution time
run_some_function()
return {'status':'ok'}
@app.route('/api/stop_function')
def stop_function():
# - there need to stop function while execution
return {'status':'ok'}
使用AJAX的简单模板:
<html>
<head>
</head>
<body>
<a href="#" class="btn" id="run_function">Run Function</a>
<a href="#" class="btn" id="stop_function">Stop Function</a>
<script>
$('#run_function').on('click', function () {
$.ajax({
url: '/api/run_function'
});
});
$('#stop_function').on('click', function () {
$.ajax({
url: '/api/stop_function'
});
});
</script>
</body>
</html>
如何在不重新加载整个服务器的情况下以编程方式中止此功能?