Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.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
Jquery 使用ajax时Flask flash消息不再有效_Jquery_Python_Ajax_Flask - Fatal编程技术网

Jquery 使用ajax时Flask flash消息不再有效

Jquery 使用ajax时Flask flash消息不再有效,jquery,python,ajax,flask,Jquery,Python,Ajax,Flask,当我使用表单操作提交表单数据时,我能够快速返回消息 查看表格: @app.route('/register') def register(): return render_template('register.html') Register.HTML <form action="{{ url_for('processRegister') }}" method=post> <dl> <dt>email:

当我使用表单操作提交表单数据时,我能够快速返回消息

查看表格:

@app.route('/register')
def register():
    return render_template('register.html')
Register.HTML

<form action="{{ url_for('processRegister') }}" method=post>
        <dl>
          <dt>email:
          <dd><input type="text" name="email" id="email">
          <div id="emailBlank" class="error"><font color="red">email cannot be blank</font></div>
          <div id="emailFormat" class="error"><font color="red">invalid email format</font></div>
          <dt>Password:
          <dd><input type="password" name="password" id="password">
          <div id="pwBlank" class="error"><font color="red">password cannot be blank</font></div>
          <dt>Enter password again:
          <dd><input type="password" name="password2" id="password2">
          <div id="pw2Blank" class="error"><font color="red">verify password field cannot be blank</font></div>
          <dd><input type="submit" value="submit" id="submit"><br><br>
        </dl>
        </form>
      <a href="{{ url_for('verifyEmailForm') }}">send another verification email</a>
    {% endblock %}
处理表单数据的我的观点:

@app.route('/processRegister', methods=['POST'])
    def processRegister():
        if request.method == 'POST':
            email = request.form['varEmail']
            flash("message -----> " + email)
        return render_template('register.html')
“我的布局”页面使用以下代码段进行闪烁:

{% with messages = get_flashed_messages() %}
  {% if messages %}
    <ul class=flashes>
    {% for message in messages %}
      <li>{{ message }}</li>
    {% endfor %}
    </ul>
  {% endif %}
{% endwith %}
{%with messages=get\u flashed\u messages()%}
{%if消息%}

{消息%中的消息为%s}
  • {{message}}
  • {%endfor%} {%endif%} {%endwith%}
    问题在于AJAX调用不会重新呈现DOM

    Javascript将作为调用的结果接收回呈现的模板

    我可以想象,在jQuery返回的响应中,flash消息就在其中

    浏览器不会将返回的值视为要呈现的内容。

    在服务器端呈现模板时,会检索并刷新消息。客户端JavaScript无法访问会话cookie。即使是,它也不容易用JavaScript解码。即使您可以对其进行解码,也会假定会话是一个cookie,而不是存储在服务器或其他什么东西上

    如果您希望呈现一些消息以响应AJAX请求,那么应该在响应中发送这些消息,并在用JavaScript编写的处理程序中呈现它们。在使用重定向时,闪现消息是为了方便起见而提供的,但由于您使用的是AJAX,因此您可以跳过这中间步骤,直接返回消息。

    您的“布局页面”中有闪现呈现代码,但它是否包含在register.html模板中?
    {% with messages = get_flashed_messages() %}
      {% if messages %}
        <ul class=flashes>
        {% for message in messages %}
          <li>{{ message }}</li>
        {% endfor %}
        </ul>
      {% endif %}
    {% endwith %}