Skip to content

Instantly share code, notes, and snippets.

@Chitrank-Dixit
Created November 9, 2013 11:35
Show Gist options
  • Save Chitrank-Dixit/7384546 to your computer and use it in GitHub Desktop.
Save Chitrank-Dixit/7384546 to your computer and use it in GitHub Desktop.
Send the Form Data as JSON in Knockoutjs
// write this under Knockout viewModel
// This is an example to send all the users comments to the server
// Basic Example we can fit it for other purposes as well
self.save = function() {
return $.ajax({
url: "comments.php",
contentType: 'application/json',
type: 'POST',
data: JSON.stringify({
'comment': self.comment(),
'name' : "Chitrank",
'uid' : "23",
}),
success: function(data) {
console.log("Pushing to comment array");
self.comments.push(new Comment({ comment: data.comment, name: data.name, uid: data.uid }));
return;
},
error: function() {
return console.log("Failed");
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment