【问题标题】:Looping out JSON into a template via the $.ajax method通过 $.ajax 方法将 JSON 循环到模板中
【发布时间】:2012-10-17 19:46:42
【问题描述】:

我在循环输出我返回的 JSON 数据时遇到问题,看起来像这样

{
 "max": "12",
 "page": null,
 "total": 12,
 "items": [
    {
    "14bfa6bb14875e45bba028a21ed38046": {
        "id": "69",
        "title": "Protein Packed Meatloaf",
        "image": "nopic.jpg",
        "link": "protein-packed-meatloaf.html",
        "description": "This meatloaf is packed with protein to help your muscles grow.",
        "category": "Dinner",
        "comment_count": "0 Comments",
        "ingredient_count": 7,
        "ate_this": 0,
        "rating": "5",
        "nutritional": {
            "calories": "205.00",
            "protein": "20.55",
            "carbohydrate": "7.70",
            "fat": "9.64"
        }
    }
    }
  ]
}

我的代码如下:

$("select,input[type=checkbox]").change(function(){ inits(); });
$("input[type=text],textarea").keystop(function(){ inits(); });

b = "form#recipeSearch";
c = $(b).attr('action') + '?r';
f = "#searchResults";

function inits(){

    console.log('Search Init');

    $.ajax({
        url: c,
        cache: false,
        dataType: 'json',
        data: $(b).serialize(),
        beforeSend: function(l) {},
        success: function(response) {

            console.log('Link: ' + c + $(b).serialize());
            //response = JSON.stringify(response);

            if (response) {
                $(f).empty();
                $.each(response, function(i,data){

                    var template = '   <div id="item-' + items.id + '" class="list-item"> '
                             + '       <a href="' + items.link + '" rel="popover" title="' + items.title + '" data-content="' + items.description + '"><img src="' + items.image + '" alt="' + items.title + '"></a>'
                             + '       <div id="title-description">'
                             + '           <h4><a href="' + items.link + '">' + items.title+ '</a></h4>'
                             + '           ' + items.rating + ' &nbsp;'
                             + '           <span class="webSymbol">,</span> Posted in <a href="' + items.category + '">' + items.category + '</a> &nbsp;'
                             + '           <span class="webSymbol">&#178;</span> ' + items.ingredient_count + ' &nbsp;'
                             + '           <span class="webSymbol">U</span> ' + items.ate_this + ' &nbsp;'
                             + '           <span class="webSymbol">c</span> ' + items.comment_count + ''
                             + '           <p id="description">' + items.description + '</p>'
                             + '       </div>'
                             + '   </div>';

                    $(f).html(template);

                });
            }
        },
        error: function(e, jqxhr, settings, exception) {
            console.log('Link: ' + c + $(b).serialize());
            console.log('Error: ' + e.toString() + ' - ' + jqxhr);
        }
    });
}

现在我不知道如何循环出每个项目,我总是得到未定义或错误。任何方向表示赞赏。

我相信来自 Chrome 的堆栈跟踪

Uncaught ReferenceError: items is not defined 
(anonymous function) min.js:521
e.extend.each jquery.min.js:2
$.ajax.success min.js:519
o jquery.min.js:2
p.fireWith jquery.min.js:2
w jquery.min.js:4
d

【问题讨论】:

  • 开始于:console.log(response);
  • 我相信应该有g 而不是response.items
  • 代码也更新了堆栈跟踪。
  • 您是否检查了控制台中的响应?

标签: javascript jquery arrays json each


【解决方案1】:

看起来你想做这样的事情:

if(response.items) { // Are there items in the response?
  $.each(response.items, function(idx,item) { // For each item in the array.
    // Now you can reference things like this.
    item.id;
    item.link;
  }
}
else {
  // Log stuff. It's a good habit to start doing.
  console.log("There are no items in the response.");
}

【讨论】:

  • 对不起,你有它,但后来我收到 -- Uncaught ReferenceError: item is not defined
  • 你是在 Chrome 还是 Firebug 中调试这个?您没有要查看的堆栈跟踪吗?您应该能够准确地看到错误出现在哪一行。
  • 好的,我做了一些更改。试试看。
  • if(production) { console.log = function() {} }
  • 那是另一个问题,当你遇到困难时应该创建一个新问题。很确定 jQuery 有一个分页插件。 archive.plugins.jquery.com/project/pagination
猜你喜欢
  • 2014-02-26
  • 2016-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多