-1

I can see 2 records in the console but my alert doesnt print out any data and doesnt append to the table either.

See below and thanks in advance

 // begin work to call across network
    var requestUri = "http://devmachine/dev/_api/Web/Lists/getByTitle('Contact')/items?$select=FirstName,FullName";
    // execute AJAX request
    $.ajax({
    url: requestUri,
    type: "GET",
    headers: { "ACCEPT": "application/json;odata=verbose" },
    success: function(data){

    //alert(data.d.FullName);
    var newRows = "";
    for (var i = 0; i < data.d.length; i++) {
       newRows += "<tr><td>" + data.d[i].FirstName + 
                  "</td><td>" + data.d[i].FullName + "</td></tr>";
    }
    $("table tr:first").after(newRows);
    alert(newRows);


    },
    error: function(){
    alert("Failed to get data");
    }
    });
  • So you're saying this "alert(data.d.FullName);" IS able to print a result? – Christoffel de Gruyter Aug 19 '14 at 8:03
  • 1
    @ChristoffeldeGruyter "my alert doesnt print out any data" – Robert Lindgren Aug 19 '14 at 8:06
  • Sorry, went over it too quickly – Christoffel de Gruyter Aug 19 '14 at 8:07
1

The data are under: data.d.result
Try this:

var newRows = "";
$(data.d.results).each(function (index) {

       newRows += "<tr><td>" + data.d.results[index].FirstName + 
                  "</td><td>" + data.d.results[index].FullName + "</td></tr>";
    });
  • Thanks guys just had a looked at the json and Yes missed results.Also when i use url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/GetByTitle('ZipCodes') i get error until i use my full path URL.Basically _spPageContextInfo.webAbsoluteUrl isnt working.Why is this? Cheers – naijacoder Aug 19 '14 at 8:14
  • In general u have to use Result when the query can return more items. If u use /item(ID) u don not have to use result. Enjoy! – Nk SP Aug 19 '14 at 8:15
  • and btw use always the browser console/debugger to watch js obj ;) – Nk SP Aug 19 '14 at 8:23
  • @naijacoder You never are able to get the _spPageContextInfo object? Or simply at page load? If it's a problem at page load, be sure to use _spbodyonloadfunctionnames.push("MyFunctionName") – Christoffel de Gruyter Aug 19 '14 at 8:39

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.