Skip to content

Instantly share code, notes, and snippets.

@aisipos
Forked from farazdagi/jsonp-in-flask.py
Created July 20, 2011 01:20
Show Gist options
  • Star 33 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save aisipos/1094140 to your computer and use it in GitHub Desktop.
Save aisipos/1094140 to your computer and use it in GitHub Desktop.
JSONP in Flask
import json
from functools import wraps
from flask import redirect, request, current_app
def support_jsonp(f):
"""Wraps JSONified output for JSONP"""
@wraps(f)
def decorated_function(*args, **kwargs):
callback = request.args.get('callback', False)
if callback:
content = str(callback) + '(' + str(f(*args,**kwargs).data) + ')'
return current_app.response_class(content, mimetype='application/javascript')
else:
return f(*args, **kwargs)
return decorated_function
# then in your view
@default.route('/test', methods=['GET'])
@support_jsonp
def test():
return jsonify({"foo":"bar"})
@hiczlf
Copy link

hiczlf commented Mar 16, 2015

cool!

@goodmami
Copy link

Response.data should not be used for recent versions of Werkzeug (and thus Flask). So the following uses get_data() and set_data(). Furthermore, using get_data(as_text=True) makes it work with Python3 as well as Python2. I also just modify the original request instead of creating a new one. Is there something wrong with doing that?

def jsonp(func):
    """Wraps JSONified output for JSONP requests."""
    @wraps(func)
    def decorated_function(*args, **kwargs):
        callback = request.args.get('callback', False)
        if callback:
            resp = func(*args, **kwargs)
            resp.set_data('{}({})'.format(
                str(callback),
                resp.get_data(as_text=True)
            ))
            resp.mimetype = 'application/javascript'
            return resp
        else:
            return func(*args, **kwargs)
    return decorated_function

@jarrekk
Copy link

jarrekk commented Dec 25, 2016

@aisipos
Based on your gist, I edit some place to make json response can contain status code, please see my fork https://gist.github.com/JiaKunUp/a9df722bad15c61ea555fa860997edc9

@LindezaGrey
Copy link

good job, helped me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment