Skip to content

Instantly share code, notes, and snippets.

@btsuhako
Created August 21, 2013 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save btsuhako/6299748 to your computer and use it in GitHub Desktop.
Save btsuhako/6299748 to your computer and use it in GitHub Desktop.
Consume JSON with a CherryPy web application
//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");
}
});
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