2012-01-14 5 views
2

これは本当に奇妙です。口ひげライブラリを使用して、私は文字通り生のオブジェクトとしてそれを解析する場合、このデータはローカルで正常に動作しますとの私の最初の時間:私のjsonが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?" 
     } 
    ] 
} 

は、ここで私が使用してヒゲのテンプレートです:

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

しかし、瞬間I 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

答えて

2

あなたが "データ型: 'JSON' を" 追加するのを忘れ、あなたのAjax呼び出しに!私は追加し、それをテストし、正常に動作します:

<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> 
+0

うん。私はちょうどそれが辛い方法を見つけた:) –

関連する問題