Django returns json data usage example

高洛峰
Release: 2017-02-22 16:47:46
Original
1587 people have browsed it

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);
});
Copy after login

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))
Copy after login


For more Django return json data usage examples and related articles, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!