Skip to content

Instantly share code, notes, and snippets.

@zachwill
Created November 18, 2011 22:32
Show Gist options
  • Save zachwill/1377985 to your computer and use it in GitHub Desktop.
Save zachwill/1377985 to your computer and use it in GitHub Desktop.
Flask JSONP decorator
"""
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