jQuery   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jquery – 使用ajax时,Flask flash消息不再有效大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
当我使用表单操作提交表单数据时,我能够回复消息.

查看表单:

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

现在我正在使用ajax我的flash消息不再出现.

$(document).ready(function(){
   (code removed for clarity)

            if (error == falsE) {
                $.ajax({
                  type: "POST",url: "/processRegister",data: { varEmail: email,varpassword: pw}
                });//ajax call
            }//end if
        });//submit
    });//load validate.js
});//doc rdy

我对处理表单数据的看法:

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

解决方法

闪烁的消息是 stored as part of the session,并在服务器端渲染模板时检索.客户端JavaScript无法访问会话cookie.即使它是,它也不容易在JavaScript中解码.即使您可以对其进行解码,也会假设会话是cookie而不是存储在服务器或其他内容上.

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

大佬总结

以上是大佬教程为你收集整理的jquery – 使用ajax时,Flask flash消息不再有效全部内容,希望文章能够帮你解决jquery – 使用ajax时,Flask flash消息不再有效所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。