Created
August 21, 2013 20:25
-
-
Save btsuhako/6299748 to your computer and use it in GitHub Desktop.
Consume JSON with a CherryPy web application
This file contains hidden or 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
//assuming that you're using jQuery | |
var myObject = { "my_key": "my_value" }; | |
$.ajax({ | |
type: "POST", | |
url: "my_route", | |
data: JSON.stringify(myObject), | |
contentType: 'application/json', | |
dataType: 'json', | |
error: function() { | |
alert("error"); | |
}, | |
success: function() { | |
alert("success"); | |
} | |
}); |
This file contains hidden or 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
import cherrypy | |
import simplejson | |
class Root: | |
@cherrypy.expose | |
@cherrypy.tools.json_out() | |
@cherrypy.tools.json_in() | |
def my_route(self): | |
result = {"operation": "request", "result": "success"} | |
input_json = cherrypy.request.json | |
value = input_json["my_key"] | |
#All responses are serialized to JSON. This the same as | |
#return simplejson.dumps(result) | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment