Skip to content

Instantly share code, notes, and snippets.

@felixzhooou
Created April 14, 2017 09:17
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 felixzhooou/211c82fbe0941e755ed91e39050b0395 to your computer and use it in GitHub Desktop.
Save felixzhooou/211c82fbe0941e755ed91e39050b0395 to your computer and use it in GitHub Desktop.
JSONP decorator based on Flask
def jsonp(func):
"""Wraps JSONified output for JSONP requests."""
@wraps(func)
def decorated_function(*args, **kwargs):
callback = request.args.get('callback', False)
if callback:
data = str(func(*args, **kwargs).data)
content = str(callback) + '(' + data + ')'
print(content)
mimetype = 'application/javascript'
return current_app.response_class(content, mimetype=mimetype)
else:
return func(*args, **kwargs)
return decorated_function
@felixzhooou
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment