Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在flask中处理javascript输出后重新加载页面_Javascript_Python_Ajax_Flask - Fatal编程技术网

在flask中处理javascript输出后重新加载页面

在flask中处理javascript输出后重新加载页面,javascript,python,ajax,flask,Javascript,Python,Ajax,Flask,我截取了一个javascript,它使用ajax向我的flask views.py脚本发送电子邮件地址。如果电子邮件不在我的数据库中,我想向该地址发送消息,或者重新加载网站并显示该电子邮件地址的用户信息。下面是我的javascript代码,它将数据发送到my views.py <script type='text/javascript'> $(".send_invite_message").click(function(evt) { var Toemail

我截取了一个javascript,它使用ajax向我的flask views.py脚本发送电子邮件地址。如果电子邮件不在我的数据库中,我想向该地址发送消息,或者重新加载网站并显示该电子邮件地址的用户信息。下面是我的javascript代码,它将数据发送到my views.py

<script type='text/javascript'>
    $(".send_invite_message").click(function(evt) {

        var Toemail = document.getElementById('To').value
        $.ajax({
            url: "/send_invitation_member",
            type: "GET",
            async: true,
            cache: false,
            dataType: 'json',
            contentType: "application/json; charset=utf-8",
            data: { email: Toemail}, 
            success: function(data) {
                ///do something
            },
        });
    });
</script>
但是ajax脚本需要返回一个json对象,所以我不知道如何重新加载站点并显示用户信息。有没有办法直接在flask中实现这一点,或者我需要扩展javascript代码并从那里加载站点? 谢谢
carl

由于AJAX响应无法直接影响调用它的页面,因此您需要稍微扩展javascript(但只需扩展一点)

在成功函数中,让我们添加以下内容:

success: function(data) {
    if (data['url'] != null) document.location = data['url'];
    else console.log('Got a valid JSON response, but no URL!');
}
此代码将页面重定向到JSON使用其“url”键指定的位置。现在剩下的就是把它添加到我们的烧瓶代码中

@app.route('/show_members')
def show_members():
    return render_template('show_members.html')

@app.route('/somewhere_else')
def some_other_route():
    return "It all works!"

@app.route('/send_invitation_member', methods=['GET'])
def send_invitation_member():

        email = request.args.get('email')
        search_result = check database for email entry

        if search_result:
            destination = url_for('.show_members')
        else:
            destination = url_for('.some_other_route')
            send message and return json object

        return Response(response=json.dumps({'url': destination}, mimetype='text/json')
我发现使用flask时,最好根据HTTP方法将路由划分为不同的函数。方法的url_是什么?这是个救命稻草。你可以在这里找到它的文档

@app.route('/show_members')
def show_members():
    return render_template('show_members.html')

@app.route('/somewhere_else')
def some_other_route():
    return "It all works!"

@app.route('/send_invitation_member', methods=['GET'])
def send_invitation_member():

        email = request.args.get('email')
        search_result = check database for email entry

        if search_result:
            destination = url_for('.show_members')
        else:
            destination = url_for('.some_other_route')
            send message and return json object

        return Response(response=json.dumps({'url': destination}, mimetype='text/json')