首页 > Ajax提交到Tornado后端Response是空白?

Ajax提交到Tornado后端Response是空白?

要实现的一个功能是post用户名密码,用Ajax得到一个status,然后确认是否传递成功,但是fire debug显示response是空白

这是handler,member是个类

pythonstatus_success = {
    "status": True
}

status_fail = {
    "status": False
}


class MainHandler(tornado.web.RequestHandler):
    def post(self, *args, **kwargs):
        username = self.get_argument("username")
        password = self.get_argument("password")
        email = self.get_argument("email")
        new_member = {
            "username": username,
            "password": password,
            "email": email
        }
        if Member.add_member(new_member):
            print(status_success)
            self.write(json_encode(status_success))
        else:
            self.write(json_encode(status_fail))

这是html单页

html<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<button id="button" type="submit">CLICK</button>

</body>
<script src="https://code.jquery.com/jquery-2.1.3.js"></script>
<script src="index.js"></script>
</html>

这是javascript文件

javascript$(document).ready(function () {
    $("#button").click(function () {
        $.post("http://localhost:8000/", {
            username: "xyz",
            password: "xyz",
            email: "xyz"
        }, function (data) {
            if(data.status == true){
                alert("hello")
            }
        });
    });
});

在控制台显示返回值空白,但是后端数据插入成功了。

但是使用命令行就会有返回值


因为跨域问题
添加如下代码即可

pythondef set_default_headers(self):
        self.set_header('Access-Control-Allow-Origin', '*')
        self.set_header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS')
        self.set_header('Access-Control-Max-Age', 1000)
        self.set_header('Access-Control-Allow-Headers', '*')
        self.set_header('Content-type', 'application/json')
【热门文章】
【热门文章】