Hi,
I am trying to bind the JSON object which came as a result of a rest webservice using knock out js and wanted to display on the screen on a table.
But some how the output is not working as expected,and not able to debug.Please let me know what is wrong in my code.
JSON object when I try to use the url mentioned below,
[{"employeeFirstName":"Divya","employeeLastName":"Potana"},{"employeeFirstName":"Mounica","employeeLastName":"Challa"}]
html file.
<html>
<head>
<title>Employee Data</title>
</head>
<body>
<script src="knockout-3.4.0.js"></script>
<script src="knockout.mapping-latest.debug.js"></script>
<table border="1">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
<tbody data-bind="foreach: employees">
<tr>
<td data-bind="text: employeeFirstName" />
<td data-bind="text: employeeLastName" />
</tr>
</tbody>
</table>
<input type="button" id="btnGetEmployees" value="Get Employees" data-bind="click: getEmployees">
</body>
<script type="application/javascript">
function MyEmployeeViewModel() {
var self = this;
self.employees = ko.observableArray();
self.getEmployees = function () {
$.ajax({
type: 'GET',
contentType: "application/javascript",
dataType: "jsonp",
success: function(data) {
var observableData = ko.mapping.fromJS(data);
var array = observableData();
self.employees(array);
}
});
};
}
ko.applyBindings(new MyEmployeeViewModel());
</script>
</html>