Skip to content

Instantly share code, notes, and snippets.

@markhuge
Created March 2, 2013 21:56
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markhuge/5073456 to your computer and use it in GitHub Desktop.
Save markhuge/5073456 to your computer and use it in GitHub Desktop.
Dynamically Serving JSONP with bottle.py
# This will return either JSON or JSONP depending on the existence of a callback.
def jsonp(request, dictionary):
if (request.query.callback):
return "%s(%s)" % (request.query.callback, dictionary)
return dictionary
@route('/something')
def something():
return jsonp(dict(success="It worked"))
# If you want to add a slightly more appropriate content type for JSONP ("application/javascript") you can,
# but it'll work either way
def jsonp(request, dictionary):
if (request.query.callback):
return "%s(%s)" % (request.query.callback, dictionary)
return dictionary
@route('/something')
def something():
if (request.query.callback):
response.content_type = "application/javascript"
return jsonp(dict(success="It worked"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment