Created
April 14, 2017 09:17
-
-
Save felixzhooou/211c82fbe0941e755ed91e39050b0395 to your computer and use it in GitHub Desktop.
JSONP decorator based on Flask
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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here it is JSONP decorator based on Flask