为什么我的 json 没有被 Mustache 解析?

发布于 2024-12-26 19:34:29 字数 1753 浏览 0 评论 0原文

这实在是太奇怪了。这是我第一次使用 Mustache 库,当我将其解析为原始对象文字时,这些数据在本地工作得很好:

{
   "datacenters":[
      {
         "title":"Flinders St Station",
         "description":"This is a pretty major train station."
      },
      {
         "title":"Southern Cross Station",
         "description":"Did you know it used to be called Spencer St Station?"
      }
   ]
}

这是我使用的 Mustache 模板:

<script id="dinfoTpl" type="text/template"> 
    {{#datacenters}}
    <h1>{{title}}</h1>
    <p>{{description}}</p>
    {{/datacenters}}    
</script>

但是当我将它放入 json 文件并尝试像这样使用 ajax 时:

<script type="text/javascript">

        var data, template, html;

        $.ajax({
            url: "datacenter.json",
            success: function(data) {
                var template = $('#dinfoTpl').html();
                var html = Mustache.to_html(template, data);
                $('#output').html(html);            
            }       
        });

</script>

我收到一条错误消息:

Uncaught TypeError: <template>:2

 >>         {{#datacenters}}
            <h1>{{title}}</h1>
            <p>{{description}}</p>
            {{/datacenters}}    

Cannot use 'in' operator to search for 'datacenters' in {
   "datacenters":[
      {
         "title":"Flinders St Station",
         "description":"This is a pretty major train station."
      },
      {
         "title":"Southern Cross Station",
         "description":"Did you know it used to be called Spencer St Station?"
      }
   ]
}

我做错了什么?

实时代码:http://bit.ly/A17pBP

This is really weird. Its my first time with using the mustache library and this data works fine locally when I parse it as a raw object literal:

{
   "datacenters":[
      {
         "title":"Flinders St Station",
         "description":"This is a pretty major train station."
      },
      {
         "title":"Southern Cross Station",
         "description":"Did you know it used to be called Spencer St Station?"
      }
   ]
}

Here's the mustache template I use:

<script id="dinfoTpl" type="text/template"> 
    {{#datacenters}}
    <h1>{{title}}</h1>
    <p>{{description}}</p>
    {{/datacenters}}    
</script>

But the moment I tuck it in a json file and try to ajax it like this:

<script type="text/javascript">

        var data, template, html;

        $.ajax({
            url: "datacenter.json",
            success: function(data) {
                var template = $('#dinfoTpl').html();
                var html = Mustache.to_html(template, data);
                $('#output').html(html);            
            }       
        });

</script>

I get an error saying:

Uncaught TypeError: <template>:2

 >>         {{#datacenters}}
            <h1>{{title}}</h1>
            <p>{{description}}</p>
            {{/datacenters}}    

Cannot use 'in' operator to search for 'datacenters' in {
   "datacenters":[
      {
         "title":"Flinders St Station",
         "description":"This is a pretty major train station."
      },
      {
         "title":"Southern Cross Station",
         "description":"Did you know it used to be called Spencer St Station?"
      }
   ]
}

What am I doing wrong?

Live code here: http://bit.ly/A17pBP

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

累赘 2025-01-02 19:34:29

您忘记在 Ajax 调用中添加“dataType: 'json'”!我添加并测试它并且工作正常:

<script type="text/javascript">

        var data, template, html;

        $.ajax({
            url: "datacenter.json",
            dataType: 'json',
            success: function(data) {
                var template = $('#dinfoTpl').html();
                var html = Mustache.to_html(template, data);
                $('#output').html(html);            
            }       
        });

</script>

You forgot to add "dataType: 'json'" to your Ajax call! I added and test it and it works fine:

<script type="text/javascript">

        var data, template, html;

        $.ajax({
            url: "datacenter.json",
            dataType: 'json',
            success: function(data) {
                var template = $('#dinfoTpl').html();
                var html = Mustache.to_html(template, data);
                $('#output').html(html);            
            }       
        });

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