I have a URL that I have been provided with that returns data in JSON. I'm trying to display the value of each of this objects using jQuery AJAX call. I keep getting 'undefined'. What am I doing wrong?
The data is as follows:
[
[
{
"label": "First",
"value": "XXXXXX"
},
{
"label": "Second",
"value": "XXXXXX"
},
{
"label": "Third",
"value": "XXXXXX"
},
{
"label": "Fourth",
"value": "XXXXXX XXX"
},
{
"label": "Fifth",
"value": "XXXXXX"
},
{
"label": "Sixth",
"value": "XXXXXX"
}
]
]
My jQuery is as follows:
$(document).ready(function(){
$.ajax({
type: 'GET',
url: 'http://url',
dataType: 'json',
success: function(data) {
var items = [];
$.each(data, function() {
var label = items.label;
var value = items.value;
$('#results').append('<p>' + label + '</p>' + '<p>' + value + '</p>');
});
}
});
return false;
});
My html is:
<div id="results">
</div>
Best Solution
The data in your JSON file is stored as an array within an array. Try this: