observable array not reflecting data on binding the output of webapi. where as it reflects data on binding hard-coded array. what is wrong in the code function GetAllCustomers.
function CustomerDataModel() {
this.customerList = ko.observableArray([
{ FirstName: "John" },
{ FirstName: "Sam" },
{ FirstName: "Peter" },
{ FirstName: "Vicky"},
{ FirstName: "Amar" }
]);
this.GetAllCustomers = function () {
$.ajax({
url: 'http://localhost/komvc/api/customer',
type: 'GET',
dataType: 'json',
contentType: 'application/json',
success: function (data) {
var mappedCusts = $.map(data, function (item) { return { FirstName: item.FirstName }; });
console.log(mappedCusts[0].FirstName);
this.customerList = ko.observableArray(this.mappedCusts);
console.log(JSON.stringify(ko.toJS(this.customerList)));
},
error: function (x, y, z) {
alert(x + '\n' + y + '\n' + z);
}
});
};
};
ko.applyBindings(new CustomerDataModel());
//html
<div>
<div data-bind="foreach: customerList">
<br />
<span data-bind='text: FirstName' />
</div>
<button id="btnGet" data-bind="click:GetAllCustomers">Get All Customers</button>
</div>
//my web api is returning
{"Id":1,"FirstName":"Rakesh","LastName":"Suresh","Phone":"919239123","Address":"Bangalore"}