Good day everybody. I have a question about how to use the right way to save data into SQL database through KnockoutJs. The record are display well in the table. It should be able to save the data via this pop-up Modal. But after I click the Create button in that modal, it only pop-up a failed Message. Can anybody please help me to solve this problem? Thank you very much.
Below is extract from main js file about Save function
var data = ko.toJSON(self.Profiles()); $.ajax({ type: 'POST', url: '/ajaxCall/insertProAjax', data: "{ Customer:" + ko.utils.stringifyJson(self.Name) + ",customerRemove:" + ko.utils.stringifyJson(self.CustomerRemove) + "}", contentType: "application/json", success: function (data) { alert("Record has been saved Successfully"); MarkCustomerAsSaved(); $('#AddNewModel').modal('hide'); }, error: function () { alert("Failed"); } }).fail(function (xhr, textStatus, err) { alert(err); });
Below is extract from the ViewModel about save function
var Customer = {}; Customer.Id = c.Id; Customer.Name = c.Name; Customer.Age = c.Age; Customer.Address = c.Address; if (isNewRecord === false) { $.ajax({ type: "PUT", url: "/api/CustomerAPI/" + c.Id, data: Customer }) .done(function (resp) { self.Message("Record Updated Successfully "); self.reset(); }) .fail(function (err) { self.Message("Error Occures, Please Reload the Page and Try Again " + err.status); self.reset(); }); } if (isNewRecord === true) { isNewRecord = false; $.ajax({ type: "POST", url: "/api/CustomerAPI", data: Customer }) .done(function (resp) { self.Message("Record Added Successfully "); self.reset(); loadData(); }).fail(function (err) { self.Message("Error Occures, Please Reload the Page and Try Again " + err.status); self.reset(); }); }
1 Answer 1
Knockout and Javascript (in this manner) are being processed client side. You will need to create something on the back end to accept your data payload and save it to the database. If you want to stay in the JavaScript family, I would recommend node.js. Alternatively this is where php, or C# would come into play.
$.ajax
wantsdata
, you're giving itData
. Those are not the same.data
andData
are two different keys. jQuery'sajax
function expectsdata
to start with a lowercase letter, just as it expectstype
,url
,contentType
andsuccess
and notType
,URL
,ContentType
orSuccess
.