叼花硬汉 2013-12-03 07:55 采纳率: 0%
浏览 34

车把不解析?

I can't for the life of me figure out what I'm doing wrong.

This is my HTML/JS:

<html>
<head>
     <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
     <script src="handlebars-v1.1.2.js"></script>
     <script>
        $(document).ready(function(){
            var jsonString = null;
            $.getJSON("data.json", function(data) {
                jsonString = data;
            });
            var source = $("#items").html();
            var template = Handlebars.compile(source);
            $("ul").append(template(jsonString));
        });
     </script>
</head>
<body>
    <script id="items" type="text/x-handlebars-template">
        <span>{{Title}} : {{CSCI}}</span>
    </script>
    <ul>
    </ul>
</body>
</html>

And this is my data.json file:

{
 "Title":"I am a thing",
 "CSCI":" "
}

The only output I get is the ":" so it's doing something properly. The console shows nothing (as in completely empty so I assume there's no syntactical errors anywhere?).

I don't like posting questions like this as it's usually because of a small mistake on my part somewhere, but I know you guys love this stuff ;)

  • 写回答

2条回答 默认 最新

  • weixin_33670713 2013-12-03 07:57
    关注

    As getJSON is an async function call you need to compile Handlebars in success callback function

        $(document).ready(function(){
            var jsonString = null;
            $.getJSON("data.json", function(data) {
                jsonString = data;
                var source = $("#items").html();
                var template = Handlebars.compile(source);
                $("ul").append(template(jsonString));
            });            
        });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?