【问题标题】:Flask redirect not work after form submit over jquery通过 jquery 提交表单后,Flask 重定向不起作用
【发布时间】:2015-09-15 18:31:31
【问题描述】:

请帮我解决问题:Flask 不想在表单提交后重定向。 代码:

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

这段代码运行良好:),auth.html 渲染表单:

{% 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 %}  

提交此表单 - 通过 js 代码:

$(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;});});     

此代码中的警报(1) - 工作。 路由/auth_echo/:

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

重定向不起作用。为什么?

【问题讨论】:

    标签: jquery python ajax flask


    【解决方案1】:

    AJAX 后不能重定向。

    如果你想重定向,你可以这样做:

     $.ajax({
                type: "POST",
                url: "http://example.com",
                dataType: 'json',
                success: function(data){
                    window.location.href = data;
                }
            });
    

    请注意,假设 data 是一个新的 url,但您可以通过 window.location.href=new_url 来“重定向”

    【讨论】:

      猜你喜欢
      • 2018-06-11
      • 1970-01-01
      • 1970-01-01
      • 2015-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多