- TurboGears 教程
- TurboGears - 首頁
- TurboGears - 概述
- TurboGears - 環境
- TurboGears - 第一個程式
- TurboGears - 依賴關係
- TurboGears - 提供模板
- TurboGears - HTTP 方法
- Genshi 模板語言
- TurboGears - 包含
- TurboGears - JSON 渲染
- TurboGears - URL 層次結構
- TurboGears - Toscawidgets 表單
- TurboGears - 驗證
- TurboGears - 快閃記憶體訊息
- TurboGears - Cookie 和會話
- TurboGears - 快取
- TurboGears - Sqlalchemy
- TurboGears - 建立模型
- TurboGears - CRUD 操作
- TurboGears - 資料網格
- TurboGears - 分頁
- TurboGears - 管理員訪問許可權
- 授權與認證
- TurboGears - 使用 MongoDB
- TurboGears - Scaffolding
- TurboGears - Hook
- TurboGears - 編寫擴充套件程式
- TurboGears - 可插入應用程式
- TurboGears - Restful 應用程式
- TurboGears - 部署
- TurboGears 實用資源
- TurboGears - 快速指南
- TurboGears - 實用資源
- TurboGears - 討論
TurboGears - JSON 渲染
@expose() 裝飾器預設呈現 html 內容。但是,可以將其設定為 json 內容型別。TurboGears 透過 tg.jsonify.JSONEncoder (**kwargs) 類支援 json 渲染。若要呈現 json 資料,只需將 json 作為內容型別傳遞給 expose 裝飾器即可。
@expose('json')
def jsondata(self, **kwargs):
return dict(hello = 'World')
如果在瀏覽器中輸入 '/jsondata' URL,它將透過響應方式顯示 −
{"hello": "World"}
jsonp 渲染
JSONP 代表帶填充的 JSON。除了它以呼叫 javascript 函式的 application/javascript 響應提供控制器返回的所有值外,它的工作方式與 json 輸出類似,該函式可提供函式引數。
要啟用 jsonp 呈現,你必須先將其新增到應用程式中的必需引擎列表中 – config/app_cfg.py −
base_config.renderers.append('jsonp')
按如下方式編寫你的 expose 裝飾器 −
@expose('json')
@expose('jsonp')
def jsonpdata (self, **kwargs):
return dict(hello = 'World')
訪問 /jsonpdata?callback = callme 時,你應看到 −
callme({"hello": "World"});
廣告