Skip to content

Instantly share code, notes, and snippets.

@arshaver
Created January 6, 2012 00:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arshaver/1568174 to your computer and use it in GitHub Desktop.
Save arshaver/1568174 to your computer and use it in GitHub Desktop.
Django JsonpResponse
#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