Home >Backend Development >Python Tutorial >Django returns json data usage example
The example in this article describes the usage of Django returning json data. Share it with everyone for your reference, the details are as follows:
1. Front-end. jQuery sends a GET request and parses the json data. The getJSON method can be found here.
url = "http://example/?question=" + question + "&rand=" + Math.random(); $.getJSON(url, function(json){ answer = json.answer; alert(answer); });
2. Backend. Django receives GET requests and returns json data.
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))
For more Django return json data usage examples and related articles, please pay attention to the PHP Chinese website!