【问题标题】:Flask redirect(urlfor()) doesn't reload the page [duplicate]烧瓶重定向(urlfor())不会重新加载页面[重复]
【发布时间】:2017-12-28 17:21:23
【问题描述】:

我有一个带有按钮的页面,该按钮使 ajax post 到 flask 路由。我想要计算消息的路由(通过示例的硬编码简化)并将其传递回另一个路由并重新加载用户所在的页面。 redirect(urlfor()) 确实调用了另一条路由,但由于页面没有重新加载,render_template 似乎没有被调用。

初始化.py

from flask import Flask, render_template, request, redirect, url_for

app = Flask(__name__)


@app.route('/second_route', methods=['POST'])
def second_route():
    print request.json
    return redirect(url_for('hello_world', message='test'))


@app.route('/')
def hello_world(message=None):
    return render_template('index.html', message=message)


app.run()

index.html

<html>
<head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>


    <script>
        $(document).ready(function () {
            $('#mybutton').click(function () {
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "/second_route",
                    data: JSON.stringify({}),
                    success: [],
                    dataType: "json"
                });

            });
        });


    </script>
</head>
<body>
{{ message }}
<button id="mybutton" name="mybutton" type="button">Click Me!</button>
</body>
</html>

【问题讨论】:

    标签: javascript python ajax python-2.7 flask


    【解决方案1】:

    AJAX 无法处理重定向响应,afaik。但是,您可以将 {{message}} 封装在 &lt;span id="message"&gt; 中,识别元素,然后执行 jQuery .html() 在 AJAX 完成成功时插入自定义文本。

    $.ajax({
       type: "POST",
       contentType: "application/json; charset=utf-8",
       url: "/second_route",
       data: JSON.stringify({}),
       success: [],
       dataType: "json"
    }).done(function(){
       $('#message').html('test');
       window.location.href = '/';
    }).fail(function(response){
       $('#message').html(response['responseText']);
    });
    

    要重定向,您可以将done 子句中的代码替换为window.location.href = "/someURL",但由于端点相同,这不是必需的。这样,就不需要使用message 变量了。

    【讨论】:

    • 所以这是一个简化的例子。 second_route 用于将数据推送到数据库。 message 用于返回该数据的成功,并且重新加载页面将从数据库中重新查询一堆东西,因此带有 div 标签的done 函数(在本示例中有效)在我的应用程序中不起作用。如果我使用window.location.href,那么我将无法访问该消息。抱歉,我的简单示例过于简单化了。
    • 只是为了澄清一下,您在最初加载index.html 时是否正在查询数据库?我认为包括两者都是你需要的。包含子句以防 AJAX 调用出错。
    • 我不确定将消息保存在done 然后重新加载/ 路由是否有效,因为这会覆盖消息。
    • 是的,我最初是在查询
    • 数据库中的任何内容都被渲染到模板中吗?因为如果是这样,那么您可以在将数据推送到同一路径后重新查询数据库,并返回一个JSON 对象(可以包含成功或失败文本)。但是,这不会重新加载页面(此时,这取决于是否需要)
    【解决方案2】:

    这就是我最终为那些阅读问题的人解决问题的方式。

    将我的发布请求更改为发布到使用此调用加载页面的同一路由:

     var posting = $.post(window.location.href, {'some key': 'some value'});
                    posting.done(function( ) {
                        window.location.replace(window.location.href);
                    });
    

    【讨论】:

    • 啊就是这样。 window.location.replace() 对我来说是新的。猜猜我们都学到了一些东西!紫外线
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-22
    • 2018-04-17
    • 1970-01-01
    • 2019-07-18
    • 1970-01-01
    • 2011-11-19
    相关资源
    最近更新 更多