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
  • 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 Commented May 15, 2017 at 9:33

1 Answer 1

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>
3
  • hey, same error :( Commented May 15, 2017 at 9:56
  • try like this data.d.results[i]["tz"] Commented May 15, 2017 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 Commented May 15, 2017 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.