-
-
Save stavinsky/54e155ede0c7da54a6ec to your computer and use it in GitHub Desktop.
cherrypy json jquery
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 is simple way to send almost any form data to cherrypy controller | |
$(function () { | |
$( "#save" ).click(function(){ | |
var $form = $(this).closest('form'); | |
$.ajax({ | |
url: "/rest/save", | |
data: formDataToJSON($form), | |
type: 'POST', | |
contentType: 'application/json', | |
dataType: 'json', | |
}).done(function(data){ | |
addAlert('Item saved by id ' + data['id']) | |
}); | |
}); | |
}); | |
function formDataToJSON($form) { | |
var data = {}, formData = $form.serializeArray() ; | |
for (var i in formData) { | |
data[formData[i].name] = formData[i].value; | |
} | |
return JSON.stringify(data); | |
}; |
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
""" | |
here is how we can get this data from json to cherrypy controoler, python side | |
""" | |
@cherrypy.expose | |
@cherrypy.tools.json_in() | |
@cherrypy.tools.json_out() | |
def save(self): | |
json = cherrypy.request.json | |
meta_id = json['meta_id'] | |
s = self.get_session() | |
meta = s.query(MetaTags).filter_by(id=meta_id).first() | |
meta.from_dict(json) | |
s.commit() | |
return dict(status='saved', id=meta.id) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment