Skip to content

Instantly share code, notes, and snippets.

@markng
Created June 25, 2009 15:49
Show Gist options
  • Save markng/135936 to your computer and use it in GitHub Desktop.
Save markng/135936 to your computer and use it in GitHub Desktop.
def json_callback(view_func):
"""if the GET parameter callback is set, wrap response in a function with that name"""
def _decorator(request, *args, **kwargs):
try:
# check for callback, if it's there, modify the return
callback = request.GET.__getitem__('callback')
response = view_func(request, *args, **kwargs)
response.content = callback + '(%s);' % response.content
return response
except KeyError, e:
# callback not found, just return the original function
return view_func(request, *args, **kwargs)
return _decorator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment