Home  >  Article  >  Backend Development  >  Django returns json data usage example

Django returns json data usage example

高洛峰
高洛峰Original
2017-02-22 16:47:461589browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn