jquery - Confused with JSON data and normal data in Django ajax request -
i read json internet still have not got grasp of it. reading article
http://webcloud.se/log/ajax-in-django-with-jquery/
i not understood first part function using json
def xhr_test(request, format): if request.is_ajax(): if format == 'xml': mimetype = 'application/xml' if format == 'json': mimetype = 'application/javascript' data = serializers.serialize(format, examplemodel.objects.all()) return httpresponse(data,mimetype) # if want prevent non xhr calls else: return httpresponse(status=400)
my main problems are
- from function getting
format
variable - does format
json
mean data given function json or data recived json - can give me simple example ouput of function
data = serializers.serialize(format, examplemodel.objects.all())
- how use data when response in jquery function
- if don't use json in above function how input , response chnage
thanks
- from function getting format variable
in practice, there lots of ways format populated. http provides accept:
header requests can use indicate preferred content-type
response. on client, might use xhr.setrequestheader('accept', 'application/json')
tell server want response in json format. in practice, though, few frameworks this. being django, arguments view functions set in urlconf, might craft urlconf this:
urlpatterns = patterns('', # ... (r'^xhr_test.(?<format>.*)$', 'path.to.xhr_test'), )
2 . format json mean data given function json or data recived json
this particular view doesn't @ request body, , providing response body in supplied format
4 . how use data when response in jquery function
depending on how complicated request needs be, can use jquery.getjson
, pass callback regular javascript objects result parsing json. if need bit more work request right, can use jquery.parsejson
process json data, , return same javascript objects.
Comments
Post a Comment