无法使用 JSONP 和 Mustache JavaScript 模板枚举对象中的部分
我正在尝试使用 Mustache.js 枚举并构建以下小提琴:
$(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 回调,它就可以正常工作。
提前致谢。
I am trying to enumerate and build the following fiddle with Mustache.js:
$(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);
});
});
Example here -> http://jsfiddle.net/mhMJA/3/
It correctly logs the JSON response to the console but then seems to be failing to build the template. If i just pass a single user to the JSONP callback it works fine.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是你的错。它的 jsfiddle 可以帮助您将 JSON 对象转换为以下格式。
我建议您编写自己的 JSONP 应用程序以避免此问题。 这里是我使用的JSON2 获取 JSON 对象。
It's not your fault. It's jsfiddle helps you convert your JSON object to the following format.
I suggest you write your own JSONP app to avoid this issue. Here is I used JSON2 to get the JSON object out.