为什么这个请求不会遵循我的龙卷风重定向
Why wont this request follow my Tornado redirect?
<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>
龙卷风代码: class 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("/")
我发送重定向请求,而不是浏览器重定向它只是获取 index.html 的 html 数据,以 msg 为单位。
您正在使用 AJAX 来检索页面,而不是命令。
假设您打开了一个网页,然后您想从另一个页面获取数据。您在新选项卡中打开该页面,服务器端告诉它重定向到"/",因此您发现自己在第二个选项卡中处于index.html
,但第一个选项卡仍保留在同一位置。
这正是您的 AJAX 正在做的事情。您已经告诉它获取页面的内容,当该页面发送重定向顺序时,它只是获取请求重定向到的页面的内容并返回该内容。
因此,AJAX 返回成功的200
状态代码,而不是重定向的301
代码。更好的解决方案是让您的服务器以唯一的响应回复,浏览器可以识别并适当地重定向。这方面的一个例子是输出一个简单的字符串,例如 redirect
,它永远不会单独出现在有效的输出中。然后可以适当地更改jQuery:
success: function(msg){
if (msg=='redirect') {
//Redirect to the index
}
else {
console.log(msg);
}
}
这将在适当的情况下重定向,否则将输出到日志。
相关文章:
- 为什么不't Javascript对我的输入值进行了一些重新检查
- 可以't让我的if语句处理js中的html表单输入
- 我的单元测试选项是什么
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 我的职位回报太快了,如何做出承诺
- 我的jQuery插件参数没有正确启动,遇到了问题
- 如何识别我的网站中的慢速设备
- 如何将JSON数据导入我的ejs模板
- JQuery添加元素需要在我的js之前再次添加JQuery脚本
- 知道为什么我的旋转木马不会自动更改图片吗
- 我的模板未被解析
- 无法将数据从firebase获取到我的html页面
- 角度图表;t显示在我的页面中
- 我的AngularJS表达式没有'不起作用
- 将电视直播频道从网站嵌入我的网站
- /undefined在我的404错误日志中多次出现
- 为什么我的d3.jsselectAll+过滤器没有过滤
- 元素在我的代码中不会.fadeTo.Don'I don’我不知道;s错误的JavaScript、JQuery、H
- 我的javascript for循环不起作用
- 为什么这个请求不会遵循我的龙卷风重定向