1

I'm trying to get all values from a list and print it to screen.

I have a column named 'tz' and I want to print it only.

I use this code, but I am getting an error:

Unable to get property '0' of undefined or null reference"

<script>
$.ajax({
    var siteurl = _spPageContextInfo.webAbsoluteUrl;
    $.ajax({
               url: siteurl + "/_api/web/lists/getbytitle('Projects')/items",
               method: "GET",
               headers: { "Accept": "application/json; odata=verbose" },
               success: function (data) {
                    if (data.d.results.length > 0 ) {
                       for (i = 0; i < data.d.results.length; i++)
                       {
                            alert(data.d.results[i].tz);
                       }
                    }       
              },
              error: function (data) {
                  alert("Error: "+ data);
             }
      });
});

  • 1
    You may want to change that alert to console.log and inspect it in F12 Dev console.... unless you really want to spend hours a day clicking OK in alert boxes – Danny '365CSI' Engelman May 15 '17 at 9:33
  • thank for the tip – uRIELk May 15 '17 at 9:57
0

You have used $.ajax({ }); 2 times. check the modified code below

 <script>
$(document).ready(function(){
    var siteurl = _spPageContextInfo.webAbsoluteUrl;
    $.ajax({
               url: siteurl + "/_api/web/lists/getbytitle('Projects')/items",
               method: "GET",
               headers: { "Accept": "application/json; odata=verbose" },
               success: function (data) {
                    if (data.d.results.length > 0 ) {
                       for (i = 0; i < data.d.results.length; i++)
                       {
                            alert(data.d.results[i].tz);
                       }
                    }       
              },
              error: function (data) {
                  alert("Error: "+ data);
             }
      });
});
</script>
  • hey, same error :( – uRIELk May 15 '17 at 9:56
  • try like this data.d.results[i]["tz"] – Suresh Bolineni May 15 '17 at 10:27
  • 1
    ["tz"] and .tz do exactly the same. Add a console.log(data) inside the succes function and check the F12 console if the data structure you expect is there – Danny '365CSI' Engelman May 15 '17 at 10:28

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.