Created
January 6, 2012 00:24
-
-
Save arshaver/1568174 to your computer and use it in GitHub Desktop.
Django JsonpResponse
This file contains 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
#adapted from http://nigel.jp/2011/06/jsonp-with-django/ | |
#JsonpResponse object | |
import json | |
from django.http import HttpResponse | |
class JsonpResponse(HttpResponse): | |
def __init__(self, data, callback): | |
json = json.dumps(data) | |
jsonp = "%s(%s)" % (callback, json) | |
HttpResponse.__init__( | |
self, jsonp, | |
mimetype='application/json' | |
) | |
#and in the view function | |
def view(request): | |
#arbitrary view code here where 'response' is the dictionary to be serialized and returned | |
return JsonpResponse(response, request.GET['callback']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment