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

发布于 2024-12-26 16:57:01 字数 1644 浏览 0 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尐偏执 2025-01-02 16:57:01

这不是你的错。它的 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 应用程序以避免此问题。 这里是我使用的JSON2 获取 JSON 对象。

It's not your fault. It's jsfiddle helps you convert your JSON object to the following format.

{"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"}

I suggest you write your own JSONP app to avoid this issue. Here is I used JSON2 to get the JSON object out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文