嗨,我正在使用 Mustache JS 来使用 JSON 获取数据。我的模板如下。
<script id="patientTabl" type="text/template">
<table class="bordered">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Phone Number</th>
<th>Patient Id</th>
<th>SSN</th>
</tr>
</thead>
<tbody>
{{#patients}}
<tr>
<td data-title="First Name">{{firstName}}<div href="#" class="tool-tip">
<span class="right-arrow">
<img class="callout" src="images/callout.gif" />
<ul>
<li><a href="update-patient.html?id={{id}}">View/Update Patient</a></li>
<li><a href="JavaScript:getInsurPlans()" id="viewInsurPlans">View Insurance Plans</a></li>
<li><a href="patient-admission.html?id={{id}}">Create Patient Admission</a></li>
</ul>
</span>
</td>
<td data-title="Last Name">{{lastName}}</td>
<td data-title="Phone Number"> {{phones}}</td>
<td data-title="Patient Id">{{id}}</td>
<td data-title="SSN">{{ssn}}</td>
</tr>
{{/patients}}
</tbody>
</table>
</script>
为此,我在下面编写 jquery 代码以将数据提取到模板中。我收到响应,但数据未插入模板中。请帮忙
$.getJSON('/ePhase-web/patients', function (data) {
var template = $('#patientTabl').html();
var html = Mustache.to_html(template, data);
$('#sampleArea').html(html);
});
最佳答案
很难说到底出了什么问题。我没有看到任何明显的错误。因此,通用调试是有序的。
大多数浏览器都有某种网络选项卡。它是否显示正在提出的请求?请求成功吗?仅当请求成功时才会调用您的方法。
如果请求成功,您可以调试代码本身。打开开发控制台后,您可以在想要暂停执行的地方放置“调试器”一词,以便您可以检查变量的值(通过将鼠标悬停在变量上)
患者选项卡的 jquery 选择是否给出了正确的元素作为结果?
“数据”是什么样的?调用 Mustache.to_html 时是否抛出错误? html 是什么样子的?
$("#sampleArea") 找到任何元素吗?
我认为逐一讨论这些问题将引导您修复错误或提出更可解决的答案。另请参阅this article on debugging in chrome
关于javascript - 使用 $.getJSON 的 Mustache JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29782137/