Skip to content

Instantly share code, notes, and snippets.

@fredj
Created July 20, 2010 12:11
Show Gist options
  • Save fredj/482876 to your computer and use it in GitHub Desktop.
Save fredj/482876 to your computer and use it in GitHub Desktop.
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
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