为什么渲染模板在ajax中不起作用

2024-09-26 22:51:43 发布

您现在位置:Python中文网/ 问答频道 /正文

为什么render\u模板不起作用我正在尝试制作一个登录表单,在成功登录后返回主页

这是我的ajax代码 你知道吗

$(document).ready(function() {
  $('.logBtn').click(function() {
    $.ajax({
      data : {
        email : $('#emailInput').val(),
        password: $('#passInput').val()
      },
      type : 'POST',
      url : '/process'
    })
    .done(function(data) {
      if (data.error) {
        $('#errorAlert').text(data.error).show();
        $('#successAlert').hide();
      }
      else {
        $('#successAlert').text(data.name).show();
        $('#errorAlert').hide();
      }
    });
    event.preventDefault();
  });
});
</script>

这是Python

@app.route('/process', methods=['POST'])
def process():

    email = request.form['email']
    password = request.form['password']

    if password and email:

        user = Db().login(email, password)

        if user:
            session['user'] = user
            session.modified = True
            return render_template("home.html", user='user')

    return jsonify({'error' : 'Incorrect username and password.'})

Tags: textdataifemailshowajaxfunctionerror

热门问题