English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Example Usage of Returning JSON Data in Django

This article describes the usage of Django returning json data. Shared for everyone's reference, as follows:

1Of front-end. jQuery sends a GET request and parses json data. For more information on the getJSON method, see here.

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

2The 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 is used to handle Chinese
  return HttpResponse(simplejson.dumps(data, ensure_ascii=False))

Readers who are interested in more content related to Python can check out the special topics on this site: 'Summary of Python Image Operation Skills', 'Python Data Structures and Algorithms Tutorial', 'Summary of Python Socket Programming Skills', 'Summary of Python Function Usage Skills', 'Summary of Python String Operation Skills', 'Classic Tutorial of Python入门与进阶', and 'Summary of Python File and Directory Operation Skills'.

I hope the content described in this article will be helpful to everyone in designing Python programs.

Statement: The content of this article is from the Internet, the copyright belongs to the original author, the content is contributed and uploaded by Internet users spontaneously, this website does not own the copyright, has not been manually edited, and does not assume relevant legal liabilities. If you find content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (when sending an email, please replace # with @ to report, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)

You May Also Like