0

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?

1
  • Also...when developing and you have an issue, probably easiest to use unminified versions of jQuery... Commented Mar 18, 2013 at 0:59

1 Answer 1

5

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){
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.