abstract:本文實例講述了Django返回json數(shù)據(jù)用法。分享給大家供大家參考,具體如下:1、前端。jQuery發(fā)送GET請求,并解析json數(shù)據(jù)。getJSON方法可參考這里。url = "http://example/?question=" + question + "&rand=" +
本文實例講述了Django返回json數(shù)據(jù)用法。分享給大家供大家參考,具體如下:
1、前端。jQuery發(fā)送GET請求,并解析json數(shù)據(jù)。getJSON方法可參考這里。
url = "http://example/?question=" + question + "&rand=" + Math.random(); $.getJSON(url, function(json){ answer = json.answer; alert(answer); });
2、后端。Django接收GET請求并返回json數(shù)據(jù)。
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))
更多關(guān)于Django返回json數(shù)據(jù)用法示例請關(guān)注PHP中文網(wǎng)(ipnx.cn)其他文章!