Python: Jak użyć AJAX w Flask za pomocą jQuery lub fetch.
HTML:
<script>
data = {'ID': 'foobar'};
$.ajax({
type: "POST",
url: "/test/ajax",
contentType: 'application/json;charset=UTF-8',
data: JSON.stringify(data, null, '\t'),
dataType: 'json',
success: function(data) {
window.location.href = data['url']
},
error: function(response) {
alert(response)
},
});
</script>
Flask:
@app.route('/test/ajax', methods=['GET', 'POST'])
def test_ajax():
if request.method == "POST":
data = request.json
print('data:', data)
result = {'url': url_for('index')}
print('result:', result)
return jsonify(result)
else:
return render_template('test-ajax.html')
Notatki:
Stackoverflow Flask with ajax response is not redirecting
If you like it
Buy a Coffee
