Created
July 19, 2012 17:39
-
-
Save zaman/3145554 to your computer and use it in GitHub Desktop.
cherry get and post
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
import cherrypy | |
import simplejson | |
class Root(object): | |
@cherrypy.expose | |
def update(self): | |
cl = cherrypy.request.headers['Content-Length'] | |
rawbody = cherrypy.request.body.read(int(cl)) | |
body = simplejson.loads(rawbody) | |
# do_something_with(body) | |
return "Updated %r." % (body,) | |
@cherrypy.expose | |
def index(self): | |
return """ | |
<html> | |
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> | |
<script type='text/javascript'> | |
function Update() { | |
$.ajax({ | |
type: 'POST', | |
url: "update", | |
contentType: "application/json", | |
processData: false, | |
data: $('#updatebox').val(), | |
success: function(data) {alert(data);}, | |
dataType: "text" | |
}); | |
} | |
</script> | |
<body> | |
<input type='textbox' id='updatebox' value='{}' size='20' /> | |
<input type='submit' value='Update' onClick='Update(); return false' /> | |
</body> | |
</html> | |
""" | |
cherrypy.quickstart(Root()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment