Flask redirects not working after form submit via jquery
Please help me with the problem: Flask doesn't want to redirect after form submission. Code:
from flask import Flask, render_template, request, redirect, url_for
....
@app.route('/auth/')
def auth():
return render_template('auth.html')
This code works well :), auth.html render form:
{% extends "system.html" %}
{% block content %}
<form id="authen">
<input type="text" id="avtor_sku" pattern="[0-9]{1,13}" maxlength="13" value='' autofocus required>
</form>
{% endblock %}
Submit this form - above the js code:
$(document).ready( function(){$('#authen').submit( function(){ var avtor_sku = $("#avtor_sku").val();
data1= '' + avtor_sku;
$.ajax({type: "GET", url: "/auth_echo/", contentType: "application/json; charset=utf-8",
data: {auth_echo_value: data1}, success: function() {alert(1)}});
return false;});});
Warning (1) in this code is work. Route / auth _echo /:
@app.route('/auth_echo/', methods=['GET'])
def auth_echo():
return redirect(url_for('openday'))
Redirection doesn't work. Why?
+3
source to share
1 answer
You cannot redirect after AJAX.
You can do something like this if you want to redirect:
$.ajax({
type: "POST",
url: "http://example.com",
dataType: 'json',
success: function(data){
window.location.href = data;
}
});
Note that the data is assumed to be the new url, but you can do window.location.href=new_url
for a "redirect"
+4
source to share