user2036066
user2036066

Reputation: 81

Error parsing fetched JSON with jQuery

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

Answers (1)

John S
John S

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

Related Questions