本文实例讲述了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数据。
Easily find JSON paths within JSON objects using our intuitive Json Path Finder
30
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))更多Django返回json数据用法示例相关文章请关注PHP中文网!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号