python笔记(6)——tornado&ajax
2013-12-29 04:35
417 查看
tornado与ajax并用,ajax用于实现前台与后台的交互。实现了一个小小的demo,应该是可以的:
$("#submitComment").click(function() {
var comment = encodeURI(encodeURI($("#comment").val()));
if(comment == "") {
alert("please input your comment!");
}
$.ajax({
type: 'POST',
url: 'http://localhost:8000/twitter/'+"{{name}}",
data: {"comment" : comment},
success: function(data) {
if(data == "1") {
showComment(comment);
}else {
alert("fail comment!");
}
}
});
});下面用$.ajax向后台传数据,success函数用于定义成功后的操作。要实现的效果是微博的无刷新评论,我的思路是将数据传到后台后,后台将数据存入数据库,若成功则返回‘1’,若不成功,则返回‘0’;ajax里面的success函数检测到后台返回的这个数据,选择是否进行评论的展示。
后台部分代码如下:
def post(self, name):
name = tornado.escape.xhtml_escape(self.current_user)
msg = self.get_argument("comment")
temp = {"auther": name, "time": "Nov 26 2013 10:30:00", "content": msg}
conn = pymongo.Connection("localhost", 27017)
db = conn.twitterDB
userSets = db.userSets
if msg is not None:
doc = userSets.find_one({"name": name})
doc['twitters'].insert(0, temp)
userSets.save(doc)
self.write("1")
$("#submitComment").click(function() {
var comment = encodeURI(encodeURI($("#comment").val()));
if(comment == "") {
alert("please input your comment!");
}
$.ajax({
type: 'POST',
url: 'http://localhost:8000/twitter/'+"{{name}}",
data: {"comment" : comment},
success: function(data) {
if(data == "1") {
showComment(comment);
}else {
alert("fail comment!");
}
}
});
});下面用$.ajax向后台传数据,success函数用于定义成功后的操作。要实现的效果是微博的无刷新评论,我的思路是将数据传到后台后,后台将数据存入数据库,若成功则返回‘1’,若不成功,则返回‘0’;ajax里面的success函数检测到后台返回的这个数据,选择是否进行评论的展示。
后台部分代码如下:
def post(self, name):
name = tornado.escape.xhtml_escape(self.current_user)
msg = self.get_argument("comment")
temp = {"auther": name, "time": "Nov 26 2013 10:30:00", "content": msg}
conn = pymongo.Connection("localhost", 27017)
db = conn.twitterDB
userSets = db.userSets
if msg is not None:
doc = userSets.find_one({"name": name})
doc['twitters'].insert(0, temp)
userSets.save(doc)
self.write("1")
相关文章推荐
- K近邻法(KNN)学习笔记(used by python & matlab)
- [Python][MachineLeaning]Python Scikit-learn学习笔记1-Datasets&Estimators
- python笔记(3)——tornado(2)
- 两个Python web框架:Django & Tornado
- python笔记3 - 布尔运算,位运算,字符串 str,转义序列,ASCII编码,len函数,隐式字符串拼接,in & not in 运算符
- ajax&&json学习笔记
- Python学习笔记 set&&dict
- Python学习笔记(四)——Python数据结构之字符串&字典
- Python学习笔记七(File&Open)
- Python开发【笔记】:git&github 快速入门
- python基础学习笔记<内建模块与第三方模块>
- python学习笔记(2)——list&tuple
- Python 学习笔记9 - Tornado
- Python笔记 - 循环&跳出循环
- Python 核心编程笔记 第七章 序列(映射&集合)
- Centos6.4(64bit)安装Python2.7.10 & tornado-4.3
- 下载配置python tornado web window7 开发笔记1:环境搭建
- 学习笔记【python】循环 zip and &
- python学习笔记--json&&pickle
- python基础学习笔记<面向对象编程>