本文实例讲述了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 HttpResponse
from django.utils import simplejson
if 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))