1

I'm trying to get items from a list usin REST and knockout and this is my code

<html lang="en-US">
<head>
<meta charset="UTF-8">
</head>
<script src="http://ajax.aspnetcdn.com/ajax/knockout/knockout-2.2.1.js"     type="text/javascript"></script>
<script src="http://code.jquery.com/jquery-1.7.1.js"></script>
<script type="text/javascript">
function insightViewModel() {
var self = this;
self.insights = ko.observableArray([]);

self.GetInsights = function () {
$.ajax({
   url: "http://demo/dev/_api/web/lists/getbytitle('news')/items",
  type: "GET",
       headers: {
       "accept": "application/json;odata=verbose",
       },
       success: function (data) {
       alert("Get data-"+data.d.results);
       self.insights.push(data.d.results);
       },
         error: function (error) {
         alert(JSON.stringify(error));
        }
     });
   }
      self. GetInsights();
   }


$(document).ready(function () {
 insightModel = new insightViewModel();
var insightDiv = document.getElementById("divDatabinding");
insightViewModel();
});
//
</script>

<body>
 <div id="divDatabinding"> 
   <table >
     <thead>
       <tr>
        <th>Name</th>
       </tr>
    </thead>
       <tbody data-bind="foreach:insights">
        <tr>                       
         <td ><span data-bind="text: Title"></span></td>
        </tr>
       </tbody>
   </table>

 </div>
</body>
 </html>

I get an alert with [![alert][1]][1]

1

Try it as below:

The javascript code:

var results = [];

function LoadStuff() {
    $.ajax({
    url: "http://demo/Dev/_api/web/lists/GetByTitle('news')/items",
    method: "GET",
    headers: {"Accept": "application/json; odata=verbose"}, 
      success: function(data){
        results.push.apply(results, data.d.results);
        ko.applyBindings(results);
      },
      error: function (fn, status, error) {
        console.log('Error:' + error);
      } 
    });
}

$(document).ready(function () {
    LoadStuff();
});

HTML:

<script src="https://ajax.aspnetcdn.com/ajax/knockout/knockout-2.2.1.js" type="text/javascript"></script>
<script src="https://code.jquery.com/jquery-1.7.1.js"></script>
<div>
    <div data-bind="template:{name:'KoList', foreach:results, as:'rows'}"></div>
        <script type="text/html" id="KoList">
          <table cellpadding="0" cellspacing="0" class="knockOutTable">
            <tr>
             <td data-bind="text:Title"></td>
          </tr>
          </table>
        </script>
</div>

Reference - Getting started with REST in SharePoint 2013

  • Not working.I dont get any errors but I don't see the items. – SharonG Feb 8 '17 at 9:09
  • 1
    Edited the code, can you check ? – Gautam Sheth Feb 10 '17 at 6:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.