javascript - 使用 $.getJSON 的 Mustache JS

标签 javascript json mustache

嗨,我正在使用 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">&nbsp;{{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/

相关文章:

javascript - 如何将 JSON 对象写入现有 JSON 文件

javascript - Ractive.js 索引似乎不是 react 性的

javascript - mustache 在服务器(rails)和客户端(javascript)上呈现

java - mustache :如何将第一个字母小写

javascript - 引用其父父级定位绝对 div

javascript - 在 JavaScript 中获取跨度的 id。

C# JSON 解析与子字符串

php - 数组 : Notice: Undefined offset: 0 的问题

javascript - 动态生成的 html 表无法获取选定的 td 值

javascript - webdriverio - 如何将 LI 文本放入数组