[py]tornado_原生ajax
2017-08-22 22:30
120 查看
[py]tornado_原生ajax
使用原生ajax偷偷发请求
# 业务逻辑处理模块 class LoginHandler(tornado.web.RequestHandler): def post(self, *args, **kwargs): pass def get(self, *args, **kwargs): self.render("login.html")
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>login</title> <link rel="stylesheet" href="{{ static_url("common.css") }}"> </head> <body> <form action="/login" method="post"> <input type="text" name="username"> <input type="password" name="password"> <input type="button" value="登录" onclick="SubmitForm();" /> </form> <script> function SubmitForm() { var xhr = new XMLHttpRequest(); xhr.open("GET","/login",true); xhr.onreadystatechange=func; xhr.send(); } function func(arg) { } </script> </body> </html>
初探ajax
ajax状态变更
当ajax状态为4时候
查看ajax接受到哪些东西
ajax发数据给后台
ajax发数据给后台-通过js获取表单数据
后端发数据给前端
前后端数据交互
完整code
后端代码#!/usr/bin/env python # coding=utf-8 import time import tornado.ioloop import tornado.web # 业务逻辑处理模块 class LoginHandler(tornado.web.RequestHandler): def post(self, *args, **kwargs): dic = {"status":True,"message":""} username = self.get_argument("username") password = self.get_argument("password") if username=="admin" and password=="admin123": pass else: dic["status"] = False dic["message"]="用户名或密码错误" import json self.write(json.dumps(dic)) def get(self, *args, **kwargs): self.render("login.html") # 配置选项模块 settings = { 'template_path': 'template', 'static_path': 'statics', } # 路由模块 application = tornado.web.Application([ (r"/login", LoginHandler), ], **settings ) ## wsgi模块 if __name__ == "__main__": application.listen(8888) tornado.ioloop.IOLoop.instance().start()
前端代码
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>login</title> <link rel="stylesheet" href="{{ static_url("common.css") }}"> </head> <body> <form action="/login" method="post"> <input id="username" type="text" name="username"> <input id="password" type="password" name="password"> <input id="submit" type="button" value="登录" onclick="SubmitForm();"/> </form> <script> xhr = null; function SubmitForm() { xhr = new XMLHttpRequest(); xhr.open("POST", "/login", true); xhr.onreadystatechange = func; xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"); // xhr.send("username=admin;password=admin123"); xhr.send("username=" + document.getElementById('username').value + ";password=" + document.getElementById('password').value); } function func() { if (xhr.readyState == 4) { console.log(1); console.log(xhr.responseText); var data = xhr.responseText; var ret_dict = JSON.parse(data); if (ret_dict.status) { } else { alert(ret_dict.message); } } } </script> </body> </html>
2017年8月22日
相关文章推荐
- 原生js方式实现ajax,并仿jquery方式简单调用
- 原生js实现AJAX(二)
- JavaScript发送原生ajax请求
- 原生AJAX入门讲解(含实例)
- 原生ajax写法 (ajax状态值)
- Ajax获取XML数据--原生JavaScript实现
- 原生AJAX调用四步曲:仅供初学者参考
- [代码]JS原生Ajax,GET和POST
- javascript原生ajax写法分享
- 使用原生ajax处理json字符串的方法
- js原生ajax与jQuery的ajax
- 原生ajax 与jquery ajax 的对比
- 原生Ajax至mini-ajax.js
- Asp.Net Ajax 学习笔记9 JavaScript的原生类型以及Microsoft AJAX Library的相关扩展(上)
- 使用原生ajax发送post请求完整案例
- Ajax练习二(原生JS异步请求)
- 原生JS写AJAX
- JavaScript原生xmlHttp与jquery的ajax方法json数据格式实例
- ExtAspNet快速入门 - 原生Ajax与PageManager控件
- Ajax原生代码