要实现的一个功能是post用户名密码,用Ajax得到一个status,然后确认是否传递成功,但是fire debug显示response是空白
这是handler,member是个类
python
status_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") } }); }); });
在控制台显示返回值空白,但是后端数据插入成功了。
但是使用命令行就会有返回值
因为跨域问题
添加如下代码即可