-
-
Save mvaz/3046388 to your computer and use it in GitHub Desktop.
Flask JSONP decorator
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
""" | |
Taken from: https://gist.github.com/1094140 | |
""" | |
from functools import wraps | |
from flask import request, current_app | |
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 + ')' | |
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