按“登录”按钮后,它会打印出整个错误.html文件到web控制台,而不是重定向到用户主页.html,哪一位出错了,干杯!代码如下。。。你知道吗
Python:
@app.route('/ValidateLogin',methods=['POST'])
def ValidateLogin():
try:
_username = request.form['username']
_password = request.form['password']
#connection to MySQL
cur = mysql.connection.cursor()
cur.callproc('sp_validateLogin', (_username,))
data = cur.fetchall()
if len(data) > 0:
if check_password_hash(str(data[0][3]), _password):
session['user'] = data[0][0]
return redirect('/UserHome')
else:
return render_template('error.html', error = 'Wrong Email Address or Password')
else:
return render_template('error.html', error = "Invalid Email Address or Password")
except Exception as e:
return render_template('error.html', error = str(e))
finally:
cur.close()
JS公司:
$(function() {
$('#btnSignIn').click(function() {
$.ajax({
url: '/ValidateLogin',
data: $('form').serialize(),
type: 'POST',
success: function(response) {
console.log(response);
},
error: function(error) {
console.log(error);
}
});
});
});
当输入正确的用户凭据时,它将重定向到
/UserHome
。你知道吗您可以通过查看如下所示的请求日志来确认:
您可以看到,由
$.ajax()
生成的对/ValidateLogin
的POST请求被302重定向到/UserHome
,然后$.ajax()
。这是自动发生的,因此您的javascript没有机会对重定向进行操作。你知道吗关于this answer,一种解决方案是让服务器发送一个JSON响应,其中包含一个“location”URL,以便在登录成功时重定向到该URL。然后让
success()
处理程序用window.location.replace(location)
重定向浏览器。你知道吗如果登录不成功,那么从
error.html
模板呈现的HTML可以在不同的JSON项中返回,success()
处理程序(因为现在应该总是得到200个响应)使用该JSON项来设置文档内容。检查引用的答案中的一些代码,这些代码显示了如何在客户端处理它。你知道吗相关问题 更多 >
编程相关推荐