Django json处理

转自:http://www.gowhich.com/blog/423

1, 发往浏览器端

前端:jQuery发送GET请求,并解析json数据。

url = "http://example.com/?q=" + q + "&rand=" + Math.random();
$.getJSON(url, function(json){
    a = json.a;
    alert(a);
});

    后端:Django接收GET请求并返回json数据。

from django.http import HttpResponse
from django.utils import simplejson
if request.method == 'GET' and 'q' in request.GET:
    question = request.GET['q']
    data = {"a": "a"}
    #ensure_ascii=False用于处理中文
    return HttpResponse(simplejson.dumps(data, ensure_ascii=False))

2,接收浏览器端

原文地址:https://www.cnblogs.com/nigang/p/3800742.html