本文實例講述了Django返回json數據用法。分享給大家供大家參考,具體如下:
1、前端。jQuery發送GET請求,并解析json數據。getJSON方法可參考這里。
url = "http://example/?question=" + question + "&rand=" + Math.random();$.getJSON(url, function(json){ answer = json.answer; alert(answer);});2、后端。Django接收GET請求并返回json數據。
from django.http import HttpResponsefrom django.utils import simplejsonif request.method == 'GET' and 'question' in request.GET: question = request.GET['question'] print(question) data = {"answer": "answer"} #ensure_ascii=False用于處理中文 return HttpResponse(simplejson.dumps(data, ensure_ascii=False))更多關于Python相關內容感興趣的讀者可查看本站專題:《Python圖片操作技巧總結》、《Python數據結構與算法教程》、《Python Socket編程技巧總結》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設計有所幫助。
新聞熱點
疑難解答
圖片精選