3

Please, help me with problem: Flask don't want redirect after form submit. Code:

from flask import Flask, render_template, request, redirect, url_for
....
@app.route('/auth/')
def auth():
return render_template('auth.html')

This code work 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 - over 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;});});     

Alert(1) in this code - work. Route /auth_echo/:

@app.route('/auth_echo/', methods=['GET'])
def auth_echo():
return redirect(url_for('openday'))

Redirect not work. Why?

1 Answer 1

4

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 is assumes data is a new url, but you can do window.location.href=new_url to "redirect"

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.