Recently in the write front end Ajax send request to the background, the result data submitted successfully, but has not executed a successful callback function, after many times to find the data, finally found the answer.
This example describes the Django return JSON data usage. Share to everyone for your reference, as follows:
1, the front-end. jquery sends a GET request and parses the JSON data. The Getjson method can be consulted here.
?
12345 |
url = "http://example/?question=" + question + "&rand=" + Math.random(); $.getJSON(url, function (json){ answer = json.answer; alert(answer); }); |
2, back end. Django receives the GET request and returns the JSON data.
?
12345678 |
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
))
|
Django returns JSON data usage example