Created
March 2, 2013 21:56
-
-
Save markhuge/5073456 to your computer and use it in GitHub Desktop.
Dynamically Serving JSONP with bottle.py
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
# 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")) |
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
# 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