This took me a while to figure out, so here it is for anyone else who’d like to know. Firstly, I was quite surprised to learn that jQuery doesn’t have a built in method for turning objects into JSON, so you’ll need the JSON parser/creator from Then you can write your javascript POST function using the jQuery ajax method. Here I’ve left the content type as text/plain, which is not strictly correct but works with the Groovy code.

function postTreeData(){
        type: "POST",
        url: "savedata",
        contentType : "text/plain",
        dataType: 'json',
        data: JSON.stringify($.tree.focused().get()),
        success: function(msg){
            alert( "Data Saved: " + msg );

And on the server end, here’s the code from the relevant controller.

def savedata = {
        def json = request.JSON
        // do something useful with the data
         render "OK"

Subscribe to articles from the programming category via RSS or ATOM


comments powered by Disqus