REST WEBSERVICE OUTPUT JSON Object binding using knock out js

75 views
Skip to first unread message

Sai Chinni

unread,
Jun 2, 2016, 7:26:34 AM6/2/16
to KnockoutJS
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>

Gunnar Liljas

unread,
Jun 2, 2016, 8:43:12 AM6/2/16
to knock...@googlegroups.com
Why are you "not able to debug"?

--
You received this message because you are subscribed to the Google Groups "KnockoutJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to knockoutjs+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

noirabys

unread,
Jun 9, 2016, 7:02:56 AM6/9/16
to KnockoutJS
hi,
i guess that the data returned is not what you expect...
try adding console.log(data) and console.log(typeof data)  to your success callback

what is observableData(); ?
Reply all
Reply to author
Forward
0 new messages