Skip to content Skip to sidebar Skip to footer

Knockout.js Save Form To Json

I have my form mapped using knockout.js and mapping plugin. I am ready to save the form to json and send it back to the server. This is my first time doing this so what is the simp

Solution 1:

To serialize your viewmodel back to JSON use ko.toJSON(myViewModel)

I would also recommend reading this post.

Edit: I may be misunderstanding what you are wanting here, but if you wanted to submit through the viewmodel you could do this:

var viewModel;
   $.getJSON('/myJSONdata', function (data) {
      viewModel = ko.mapping.fromJS(data);
      viewModel.doSomething = function(){
        var jsonData = ko.mapping.toJSON(viewModel);   
        $.ajax({
           type: "POST",
           url: '/myJSONdata',
           data: jsonData
        });
      };
      ko.applyBindings(viewModel);
    });

Post a Comment for "Knockout.js Save Form To Json"