Created
August 27, 2014 09:04
-
-
Save janzhen/0504920a1251ca9f787b to your computer and use it in GitHub Desktop.
Flask jsonp decorator.
This file contains hidden or 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
| from functools import wraps | |
| def jsonp(func): | |
| """Wraps JSONified output for JSONP requests.""" | |
| @wraps(func) | |
| def decorated_function(*args, **kwargs): | |
| from flask import request, current_app | |
| import re | |
| callback = request.args.get('callback', False) | |
| if callback and re.match('^\w+$', callback): | |
| data = str(func(*args, **kwargs).data) | |
| content = '/**/%s(%s)' % (callback, data) | |
| 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