REST API - 'results' undefined

I have a list and when the page loads I want to grab the items from my list and add it to an array. I am getting an error when I try to read the rest data.  Here's what i have so far.

<script>
$(document).ready(function() { 
	var iceCreamApp = {};
	iceCreamApp.Truck = new Array();
	
	//Load data from list into array right away
	(function() {
		var url = "http://isaac.issharepoint.com/demo/_vti_bin/listdata.svc/IceCreamTrucks";
		$.ajax({
			url:url,
			method:"GET",
			headers: {"Accept": "application/json; odata=verbose"},
			success: function(data) {
				success(data.d);
			},
			error: function(data) {
				failure(data.responseJSON.error);
			}
		})
			
			function success(data)
			{
				console.log(data.d.results);
			}
	}());
	
	
})</script>

Open in new window


Here's the error I get
SCRIPT5007: Unable to get property 'results' of undefined or null reference

Points to this line
console.log(data.d.results);
Any help would be great. thanks
LVL 5
IsaacSharePoint Client Side DeveloperAsked:
Who is Participating?
 
Jamie McAllister MVPSharePoint ConsultantCommented:
How do you know it succeeds when the failure call is commented out?

In the Success line why not address a specific property of the returned data for display?

or just try this; alert(data.d);
0
 
Jamie McAllister MVPSharePoint ConsultantCommented:
It's a little typo I think;

You call Success like this;

success(data.d);

But the signature is this;

function success(data)

So you're effectively calling console.log(data.d.d.results); in your code, hence the null ref.
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
Thanks but now I get a blank page.  None of the data is rendering in the console.
Here's the updated code:

(function() {
		var url = "http://isaac.issharepoint.com/demo/_api/web/lists/GetByTitle('IceCreamTrucks')/items?$select=State";
		$.ajax({
			url:url,
			type:"GET",
			dataType:"json",
			headers: {"Accept": "application/json; odata=verbose"},
			success: function(data) {
				success(data);
			},
			error: function(data) {
				//failure(data);
			}
		})
			
			function success(data)
			{
				console.log(data);
			}
	}());

Open in new window

0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
When I had the failure line uncommented, that's what rendered. I will try data.d
0
 
IsaacSharePoint Client Side DeveloperAuthor Commented:
That still does not work for me.  Here's the page below.
http://isaac.issharepoint.com/demo/SitePages/iceCream.aspx
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.