Created
March 22, 2015 16:53
-
-
Save jayapal/93f783d3550eaf1555ab to your computer and use it in GitHub Desktop.
Django listview as JSON and Jquery getJSON example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class JsonResponseMixin(object): | |
""" | |
Return json | |
""" | |
def render_to_response(self, context): | |
queryset = self.model.objects.all() | |
data = serializers.serialize('json', queryset) | |
return HttpResponse(data, content_type='application/json') | |
JSON parsing using getJSON | |
$.getJSON('/your/url/', function(data) { | |
$.each(data, function(index) { | |
alert(data[index].TEST1); | |
alert(data[index].TEST2); | |
}); | |
}); | |
// you can use http://api.jquery.com/jquery.get/ | |
$.ajax({ | |
url: url, | |
data: data, | |
success: success, | |
dataType: dataType | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment