0

jQueryでJSONファイルを解析しようとしています。新しいカテゴリ (最初の文字を除く) の各 image_url を返し、ページに画像を配置したいと考えています。

これが私のコードです

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');
};

そして、クロムインスペクターでこのエラーを返します

Uncaught TypeError: Cannot call method 'substring' of undefined

なぜ未定義なのですか?

4

1 に答える 1

5

コールバック関数の最初のパラメーター$.each()はインデックスで、2 番目のパラメーターは値です。

試す:

$.each(data.products_and_categories.new, function(index, item){
于 2013-03-18T00:58:54.300 に答える