常见登陆页后端和前端的操作

后端
#登陆页面
@app.route("/sing_in")
def sing_in():
return render_template("sign_in.html")
#路由验证逻辑,使用路由由路由传参的形式
@app.route("/checkuser/username=<username>/password=<password>",methods=['GET','POST'])
#路由方法的参数必须和网址中的参数一致
def checkuser(username,password):
#在数据库中查找该用户
res = db.user.find_one({'username':username,'password':str(password)})
#判断是否存在
if res is None:
return jsonify({'result':0})
else:
return jsonify({'result':1})

#前端
<script>
//写一个方法来提交用户名和密码
function sing_in(){
//获取用户名
var username = $("#username").val();
//获取密码
var password = $("#password").val();
//拼接url
var url = '/checkuser_mysql/username='+username+'/password='+password;
//发送获取数据
$.getJSON(url,function(msg){
if (msg.result == 0){
alert("用户名或密码错误");
}else{
alert("登陆成功");
window.location.href("/");
}

});
}

</script>
原文地址:https://www.cnblogs.com/antique/p/10216534.html