Created
September 17, 2013 13:22
-
-
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.
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(): | |
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