Skip to content

Instantly share code, notes, and snippets.

@adewes
Created September 17, 2013 13:22
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 adewes/6594226 to your computer and use it in GitHub Desktop.
Save adewes/6594226 to your computer and use it in GitHub Desktop.
A Flask decorator that takes a response with JSON content and converts it to a well-formatted JSONP response.
def jsonp():
def decorator(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if not 'callback' in request.args:
abort(404)
response = f(*args, **kwargs)
if isinstance(response,str):
response = make_response(response)
response.data = "%s(%s)" % (request.args['callback'],response.data)
response.mimetype = 'application/javascript'
return response
return decorated_function
return decorator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment