hula919 2017-02-08 08:03 采纳率: 0%
浏览 3133

tornado登陆跳转问题 用ajax提交post请求

用ajax提交post请求后通过验证服务器返回跳转地址,然后又莫名出现了get请求跳回login页面。服务器log如下:

 [I 170208 15:58:10 web:1971] 200 GET /login (::1) 2.00ms
test1 123456
[I 170208 15:58:19 web:1971] 302 POST /login (::1) 3.00ms
[I 170208 15:58:19 web:1971] 304 GET / (::1) 2.00ms
[I 170208 15:58:19 web:1971] 200 GET /login?_xsrf=2%7Ceaa8943a%7C09432d5ffa3cd4bc80fe8232a2f5e89e%7C1486526704 (::1) 2.00ms

很好奇?_xsrf=27C09432d5ffa3cd4bc80fe8232a2f5e89e%7C148如何添加进去的

index.py

 #!/usr/bin/env Python
# coding=utf-8

import tornado.escape
import methods.db as mrd
from handlers.base import BaseHandler

class LoginHandler(BaseHandler):
    def get(self):
        self.render('login.html')

    def post(self):
        username = self.get_argument("username")
        password = self.get_argument("password")
        print(username,password)
        user_infos = mrd.select_table(table="users",column="*",condition="username",value=username)
        if user_infos:
            db_pwd = user_infos[0][2]
            if db_pwd == password:
                self.set_current_user(username)
                self.redirect("/")
            else:
                self.write("2")
        else:
            self.write("1")

    def set_current_user(self, user):
        print(user)
        if user:
            self.set_secure_cookie('user', tornado.escape.json_encode(user))#注意这里使用了 tornado.escape.json_encode() 方法
            #print(tornado.escape.json_decode(self.current_user))
        else:
            self.clear_cookie("user")


class WelcomeHandler(BaseHandler):
    @tornado.web.authenticated
    def get(self):
        username = tornado.escape.json_decode(self.current_user)
        self.render('index.html', user=username)
        print(username)

login.html

 <!DOCTYPE html>
<html lang="zh-cn">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="{{static_url("css/bootstrap.min.css")}}" />
    <style>
        body{
            padding: 70px 0;
        }
        .pos_m{
            margin-top: 10%;
            margin-bottom: 10%;
        }
    </style>
  </head>
  <body>
    <div class="container pos_m">
        <div class="row justify-content-center">
            <div class="col-lg-3 col-md-5 col-sm-8 ">
                <form class="form-signin">
                    {% module xsrf_form_html() %}
                    <h2 class="form-signin-heading">Please sign in</h2>
                    <div class="form-group">
                        <label for="InputUserName" class="sr-only">username</label>
                        <input type="text" class="form-control form-control-danger" placeholder="Username" id="username">
                        <label for="InputPassword" class="sr-only">password</label>
                        <input type="password" class="form-control" placeholder="Password" id="password">
                    </div>
                    <div class="checkbox">
                        <label>
                          <input type="checkbox"> Remember me
                        </label>
                    </div>
                    <button type="submit" class="btn btn-primary btn-block" id="login">Submit</button>
                </form>
            </div>
        </div>
    </div>

  </body>
</html>

script.js

  function getCookie(name){
    var x = document.cookie.match("\\b" + name + "=([^;]*)\\b");
    return x ? x[1]:undefined;
}

$(document).ready(function(){
    $("#login").click(function(){
        var user = $("#username").val();
        var pwd = $("#password").val();
        var pd = {"username":user, "password":pwd, "_xsrf":getCookie("_xsrf")};
        $.ajax({
            type:"post",
            url:"/login",
            data:pd,
            cache:false,
            success:function(data){
                alert(data);
            }
        });
    });
});

菜鸟入门 求大神指教,调试一天了,实在不知道问题在哪里。

  • 写回答

1条回答

  • zqbnqsdsmd 2017-02-18 15:50
    关注
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况