Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@skovhus
Forked from farazdagi/jsonp-in-flask.py
Last active December 18, 2019 08:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skovhus/6218772 to your computer and use it in GitHub Desktop.
Save skovhus/6218772 to your computer and use it in GitHub Desktop.
from functools import wraps
from flask import request, current_app, jsonify
def support_jsonp(f):
"""Wraps output to JSONP"""
@wraps(f)
def decorated_function(*args, **kwargs):
result = jsonify(f(*args, **kwargs))
callback = request.args.get('callback', False)
if callback:
content = str(callback) + '(' + str(result.data) + ')'
return current_app.response_class(content,
mimetype='application/json')
else:
return result
return decorated_function
# then in your view
@app.route('/test/<task_id>', methods=['GET'])
@support_jsonp
def test(id):
return {"foo": "bar", "id": id}
@mohamedNCIR
Copy link

Hi all , how can I get the reponse of the web service in a php callback function

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