Reputation: 81
I'm trying to parse a JSON file with jQuery. I want to return each image_url in the new category (minus the first character) and put the image on the page.
Here's my code
function redo () {
$('#init').addClass('disabled');
$.getJSON('http://server/new/sample.json', function(data) {
$.each(data.products_and_categories.new, function(item){
$("<img/>").attr("src", "http://d2flb1n945r21v.cloudfront.net/production/uploaded/style/" + item.image_url.substring(1)).appendTo("#items");
});
});
$('#init').removeClass('disabled');
};
and it returns this error in the chrome inspector
Uncaught TypeError: Cannot call method 'substring' of undefined
Why is it undefined?
Upvotes: 0
Views: 108
Reputation: 21482
The first parameter to the callback function for $.each()
is the index, the second parameter is the value.
Try:
$.each(data.products_and_categories.new, function(index, item){
Upvotes: 5