017-浏览器返回数据
1.json数据
路由设置
(r'/json2', index.Jeson2Handler)
代码:
1 from tornado.web import RequestHandler 2 class Jeson2Handler(RequestHandler): 3 def get(self): 4 per={ 5 "name":"sunck", 6 "age":18, 7 "height":175, 8 "weight":70 9 } 10 11 self.write(per)
上述代码等价于:
1 import json 2 from tornado.web import RequestHandler 3 class Jeson2Handler(RequestHandler): 4 def get(self): 5 per={ 6 "name":"sunck", 7 "age":18, 8 "height":175, 9 "weight":70 10 } 11 #将 字典转为 jeson字符串 12 jsonStr=json.dumps(per) 13 #在浏览器检查中(Headers--Response Headers),请求头显示AAA:999 14 self.set_header("AAA","999") 15 # 在浏览器检查中(Headers--Response Headers),将Content-Type 参数 值 改为a 16 #self.set_header("Content-Type","a") 17 18 #返回 jeson字符串 19 self.write(jsonStr)
补充:
#预先设置响应头 def set_default_headers(self): self.set_header("Content-Type","text/html: charset=UTF-8")
===今日我言===========
勇气 让人进步
===================