繁体   English   中英

从URL获取的json数据未在handlebars.js中解析

[英]json data getting from url is not parsed in handlebars.js

我想解析从车把中的url获得的JSON数据。 我尝试过的是从URL获得JSON数据。 我将其定义为数据对象。 我想知道如何使用handlebars.js解析数据。我是handlebars.js的新手,是否可以通过其他方法来定义每个属性? 因为我的JSON数据很大。 例如

reportData = {
        inquiryId= data.data[0].inquiryId;
}

HTML代码:

<script id="address-template" type="text/x-handlebars-template">
  {{#with data}}
  <p> My id is  {{{inquiryId}}}</p>
  {{/with}}
</script>
<div class="content-placeholder"></div>

JS代码:

var reportData= {};
    $(document).ready(function () {
        $.ajax({
            type:'GET',
            url: reportURL,
            success : function (data){
                var inquiryId= data.data[0].inquiryId;

                var theTemplateScript = $("#address-template").html();
                console.log(theTemplateScript);
                // Compile the template
                var theTemplate = Handlebars.compile(theTemplateScript);

               // Define our data object
                reportData=data;
                console.log(reportData);
                // Pass our data to the template
                var theCompiledHtml = theTemplate(reportData);

                // Add the compiled html to the page
                $('.content-placeholder').html(theCompiledHtml);
            }

        })
    });

JSON:

{  
   "success":true,
   "errors":{  

   },
   "authenticated":true,
   "program":1,
   "data":[  
      {  
         "id":1,
         "date":1505756267000,
         "name":"AKKAYA, JORGE",
         "productName":"Credit Profile",
         "inquiryId":726608
      }
   ]
}

我的输出是:我的id是

谁能帮我吗? 提前致谢。

在您的Json中,数据包含数组,而在html中,您将其视为单个对象。 因此,请使用以下车把格式对其进行迭代。

{{#with abc}}
  {{#each this}}
    <p> My id is  {{{inquiryId}}}</p>
  {{/each}}
{{/with}}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM