Skip to content
Myr

Removed padded JSON (JSONP) again.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

The implementation was not clean and generally the needs for padded json are disappearing now that all browsers support cross site communication with the regular xmlhttprequest.

authored by Armin Ronacher · committed by Armin Ronacher · parent 6809ffc · Browse files · 2 files+1 added−44 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+1 added−44 removed

  • other:+1 added−44 removed

2 files+1 added−44 removed

modified:flask/testsuite/helpers.py+1 added-18 removed
71 unmodified lines
72 return flask.jsonify(**d)73 @app.route('/dict')74 def return_dict():75 return flask.jsonify(d)76 @app.route("/unpadded")77 def return_padded_false():78 return flask.jsonify(d, padded=False)79 @app.route("/padded")80 def return_padded_true():81 return flask.jsonify(d, padded=True)82 @app.route("/padded_custom")83 def return_padded_json_custom_callback():84 return flask.jsonify(d, padded='my_func_name')76 c = app.test_client()86 for url in '/kw', '/dict', '/unpadded':77 for url in '/kw', '/dict':78 rv = c.get(url)79 self.assert_equal(rv.mimetype, 'application/json')80 self.assert_equal(flask.json.loads(rv.data), d)90 for get_arg in 'callback=funcName', 'jsonp=funcName':91 rv = c.get('/padded?' + get_arg)92 self.assert_( rv.data.startswith("funcName(") )93 self.assert_( rv.data.endswith(")") )94 rv_json = rv.data.split('(')[1].split(')')[0]95 self.assert_equal(flask.json.loads(rv_json), d)96 rv = c.get('/padded_custom?my_func_name=funcName')97 self.assert_( rv.data.startswith("funcName(") )8182 def test_json_attr(self):83 app = flask.Flask(__name__)84 @app.route('/add', methods=['POST'])
323 unmodified lines
modified:flask/helpers.py-26 removed
116 unmodified lines
117 security reasons only objects are supported toplevel. For more118 information about this, have a look at :ref:`json-security`.119120 .. versionadded:: 0.2121122 .. versionadded:: 0.9123 If the ``padded`` argument is true, the JSON object will be padded124 for JSONP calls and the response mimetype will be changed to125 ``application/javascript``. By default, the request arguments ``callback``126 and ``jsonp`` will be used as the name for the callback function.127 This will work with jQuery and most other JavaScript libraries128 by default.129130 If the ``padded`` argument is a string, jsonify will look for131 the request argument with the same name and use that value as the132 callback-function name.121 """122 if __debug__:123 _assert_have_json()136137 padded = kwargs.get('padded', False)138 if 'padded' in kwargs:139 del kwargs['padded']140141 if padded:142 if isinstance(padded, str):143 callback = request.args.get(padded) or 'jsonp'144 else:145 callback = request.args.get('callback') or \146 request.args.get('jsonp') or 'jsonp'147 json_str = json.dumps(dict(*args, **kwargs), indent=None)148 content = str(callback) + "(" + json_str + ")"149 return current_app.response_class(content, mimetype='application/javascript')124 return current_app.response_class(json.dumps(dict(*args, **kwargs),125 indent=None if request.is_xhr else 2), mimetype='application/json')126127
681 unmodified lines