前端代码:<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>wp231957个人blog登录页面</title>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"> </script>
<style>
label { float: left; width: 10em; margin-right: 1em; text-align: right; }
#logtit {margin-left:180px;}
#logclick {margin-left:180px;}
#logout {margin-left:30px;}
div{ border:1px solid blue}
#divlogin {margin-top:250px ;width:500px;margin-left:400px;height:180px}
</style>
</head>
<body>
<div id ="divlogin">
<h3 id="logtit" >用户登录界面</h3>
<form method="POST">
<label>用户名:</label><input type="text" name="username" id="usr" value=""><br>
<label>密   码:</label><input type="password" id="pwd" name="password" value=""><br><br>
<input id="logclick" type="button" value="登录">
<input id="logout" type="button" value="退出">
</form>
</div>
<script>
onload=function(){
document.getElementsByName("username")[0].focus()
$('body').on('click' , '#logclick' , function()
{
var username = $("#usr").val();
var pwd = $("#pwd").val();
$.ajax
({
type: "post",
url: "/session",
data: JSON.stringify({'usr':username,'pwd':pwd}),
contentType: 'application/json;charset=utf-8',
dataType : "json",
success:function(hdata)
{
if(hdata)
{
console.log(hdata);
}
else
{
console.log("失败了");
}
}
});
});
}
</script>
</body>
</html>
后台:
@app.route('/session',methods=['GET','POST'])
def get():
data = json.loads(request.get_data(as_text=True))
print(data) #这里能正常打印前端传入的用户名密码
username=data["usr"]
pwd=data["pwd"]
if not session.get('username'):
session["username"]=username
session.permanent = True
return render_template("lybmain.html",username=username)
这个lybmain.html 就是想要重定向的url |