gpt4 book ai didi

javascript - 无法使用 JSONP 和 Mustache JavaScript 模板枚举对象中的部分

转载 作者:行者123 更新时间:2023-12-02 19:54:21 25 4
gpt4 key购买 nike

我正在尝试使用 Mustache.js 枚举并构建以下 fiddle :

$(function () {
var choices = { "users": [
{ "first_name": "Ryan",
"last_name": "Pays",
"pic_square": "/Global/profile/thumb/placeholder.jpg",
"product_name": "Merlin - the complete box set",
"product_picture": "/Global/products/full/box-set.jpg"
},
{ "first_name": "Eric",
"last_name": "Li Koo",
"pic_square": "/Global/profile/thumb/placeholder.jpg",
"product_name": "Merlin - Series 4 volume 1",
"product_picture": "/Global/products/full/box-set.jpg"
},
{ "first_name": "Abdul",
"last_name": "Raouf",
"pic_square": "/Global/profile/thumb/placeholder.jpg",
"product_name": "Merlin - the complete box set",
"product_picture": "/Global/products/full/box-set.jpg"
}]
};
$.getJSON("http://jsfiddle.net/echo/jsonp/?callback=?", choices, function (data) {
console.log(data);
var template = "<ul>{{#users}}" +
"<li>" +
"<p><strong>{{first_name}} {{last_name}}</strong> likes {{product_name}}</p>" +
"</li>" +
"{{/users}}</ul>",
html = Mustache.to_html(template, data);
$('.wrapper').html(html);
});
});

示例在这里 -> http://jsfiddle.net/mhMJA/3/

它正确地将 JSON 响应记录到控制台,但似乎无法构建模板。如果我只是将单个用户传递给 JSONP 回调,它就可以正常工作。

提前致谢。

最佳答案

这不是你的错。它的 jsfiddle 可以帮助您将 JSON 对象转换为以下格式。

{"users[0][first_name]":"Ryan","users[1][first_name]":"Eric","users[2][first_name]":"Abdul","users[2][product_picture]":"/Global/products/full/box-set.jpg","users[1][product_picture]":"/Global/products/full/box-set.jpg","users[1][pic_square]":"/Global/profile/thumb/placeholder.jpg","users[1][product_name]":"Merlin - Series 4 volume 1","users[0][last_name]":"Pays","users[0][product_picture]":"/Global/products/full/box-set.jpg","users[1][last_name]":"Li Koo","users[0][product_name]":"Merlin - the complete box set","users[0][pic_square]":"/Global/profile/thumb/placeholder.jpg","users[2][last_name]":"Raouf","users[2][pic_square]":"/Global/profile/thumb/placeholder.jpg","users[2][product_name]":"Merlin - the complete box set","_":"1326530878282"}

我建议您编写自己的 JSONP 应用程序以避免此问题。 Here我用的是JSON2获取 JSON 对象。

关于javascript - 无法使用 JSONP 和 Mustache JavaScript 模板枚举对象中的部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8850219/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com