<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.0.js"></script>
<title>
Login page
</title>
</head>
<body>
<h1 style="font-family:Comic Sans Ms;text-align"="center";font-size:20pt;
color:#00FF00;>
Simple Login Page
</h1>
<form name="login">
Username<input type="text" name="userid"/>
Password<input type="password" name="pswrd"/>
<input type="button" onclick="check(this.form)" value="Login"/>
<input type="reset" value="Cancel"/>
</form>
<script language="javascript">
function check(form)
{
$.ajax({
type: 'POST',
url: '/auth/login',
data: {
'UserName': form.userid.value,
'Password': form.pswrd.value // <-- the $ sign in the parameter name seems unusual, I would avoid it
},
success: function(msg){
console.log(msg);
}
});
}
</script>
</body>
</html>
Tornado 代码: 类 MainHandler(BaseHandler): @tornado.web.authenticated def get(self): self.render("index.html")
class AuthLoginHandler(BaseHandler):
@tornado.web.asynchronous
def get(self):
self.render("login.html")
def post(self):
username = self.get_argument("UserName",strip = True)
password = self.get_argument("Password",strip = True)
user = auth_actions.do_login(db,username,password)
if not user:
raise tornado.web.HTTPError(500, "Authentication Failed")
self.set_secure_cookie("user", tornado.escape.json_encode(user))
self.redirect("/")
我发送重定向请求,而不是浏览器重定向它只是在 msg 中获取 index.html 的 html 数据。