We help IT Professionals succeed at work.

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
Comment
Watch Question

Jamie McAllisterCloud Expert
Most Valuable Expert 2013
Top Expert 2014

Commented:
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.
IsaacSharePoint Client Side Developer

Author

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

Cloud Expert
Most Valuable Expert 2013
Top Expert 2014
Commented:
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);
IsaacSharePoint Client Side Developer

Author

Commented:
When I had the failure line uncommented, that's what rendered. I will try data.d
IsaacSharePoint Client Side Developer

Author

Commented:
That still does not work for me.  Here's the page below.
http://isaac.issharepoint.com/demo/SitePages/iceCream.aspx