Add jsonp support inside of jsonify
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Christoph Heer · committed by Ned Jackson Lovely · parent 7ed3cba · Browse files · 2 files·+35 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+35 added−0 removed
- other:+35 added−0 removed
Provenance
- Commit
cb24646- Tree
047d641- Author
Christoph Heer <Christoph.Heer@gmail.com>- Committer
Ned Jackson Lovely <njl@njl.us>- Parent
7ed3cba
2 files changed+35 added−0 removed
modified:flask/testsuite/helpers.py+14 added
59 unmodified lines60 return flask.jsonify(**d)61 @app.route('/dict')62 def return_dict():63 return flask.jsonify(d)64+ @app.route("/padded")65+ def return_padded_json():66+ return flask.jsonify(d, padded=True)67+ @app.route("/padded_custom")68+ def return_padded_json_custom_callback():69+ return flask.jsonify(d, padded='my_func_name')70 c = app.test_client()71 for url in '/kw', '/dict':72 rv = c.get(url)73 self.assert_equal(rv.mimetype, 'application/json')74 self.assert_equal(flask.json.loads(rv.data), d)75+ for get_arg in 'callback=funcName', 'jsonp=funcName':76+ rv = c.get('/padded?' + get_arg)77+ self.assert_( rv.data.startswith("funcName(") )78+ self.assert_( rv.data.endswith(")") )79+ rv_json = rv.data.split('(')[1].split(')')[0]80+ self.assert_equal(flask.json.loads(rv_json), d)81+ rv = c.get('/padded_custom?my_func_name=funcName')82+ self.assert_( rv.data.startswith("funcName(") )83 84 def test_json_attr(self):85 app = flask.Flask(__name__)86 @app.route('/add', methods=['POST'])287 unmodified linesmodified:flask/helpers.py+21 added
115 unmodified lines116 security reasons only objects are supported toplevel. For more117 information about this, have a look at :ref:`json-security`.118 119 .. versionadded:: 0.2120+121+ .. versionadded:: 0.9122+ If the argument ``padded`` true than the json object will pad for123+ JSONP calls like from jquery. The response mimetype will also change124+ to ``text/javascript``.125+126+ The json object will pad as javascript function with the function name 127+ from the request argument ``callback`` or ``jsonp``. If the argument128+ ``padded`` a string jsonify will look for the function name in the129+ request argument with the name which is equal to ``padded``. Is there130+ no function name it will fallback and use ``jsonp`` as function name.131 """132 if __debug__:133 _assert_have_json()134+ if 'padded' in kwargs:135+ if isinstance(kwargs['padded'], str):136+ callback = request.args.get(kwargs['padded']) or 'jsonp'137+ else:138+ callback = request.args.get('callback') or \139+ request.args.get('jsonp') or 'jsonp'140+ del kwargs['padded']141+ json_str = json.dumps(dict(*args, **kwargs), indent=None)142+ content = str(callback) + "(" + json_str + ")"143+ return current_app.response_class(content, mimetype='text/javascript')144 return current_app.response_class(json.dumps(dict(*args, **kwargs),145 indent=None if request.is_xhr else 2), mimetype='application/json')146 147 562 unmodified lines