Created
July 20, 2010 12:11
-
-
Save fredj/482876 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pylons import request, response | |
from simplejson import dumps | |
def jsonify(data): | |
func = request.params.get('jsonp') or \ | |
request.params.get('callback') or \ | |
request.params.get('cb') | |
body = dumps(data, indent=None if request.is_xhr else 2) | |
response.charset = 'utf8' | |
if func is not None: | |
response.content_type = 'application/javascript' | |
return "%s(%s);"%(func, body) | |
else: | |
response.content_type = 'application/json' | |
return body | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FooController(BaseController): | |
def index(self): | |
# ... | |
return jsonify({'foo': 'bar'}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment