如何从ajax成功传递数据并在把手模板中呈现?

编程入门 行业动态 更新时间:2024-10-10 00:20:59

如何从ajax成功传递数据<a href=https://www.elefans.com/category/jswz/34/1771370.html style=并在把手模板中呈现?"/>

如何从ajax成功传递数据并在把手模板中呈现?

我在.hbs文件中拥有这个

                    <div class="col-12 col-md-3 order-md-1">
                        <div id="notes">
                            {{#each notes}}
                                {{>note}}
                            {{/each}}
                        </div>
                    </div>

这是我的jquery:

$.ajax({
    url: '/search/notes',
    method: 'GET',
    data: {
        search_string: 'server'
    },
    success: function(response) {
        var source = $('#notes').html()
        var template = Handlebarspile(source)
        var context = {
            notes : response.notes,
        }
        var el_html = template(context);
        console.log(el_html)
        $('#notes').html(el_html)
    }
})

<script src=".js/2.0.0/handlebars.js"></script>

我无法在notes上获得id="notes"。结果为空白div。任何帮助吗?

回答如下:

可能与您的Ajax响应有关。下面的示例在没有Ajax的情况下工作。

setTimeout(function() {
var source = $('#notes').html()
var template = Handlebarspile(source)
var context = {
    notes : ["test", "test2"],
}
var el_html = template(context);
console.log(el_html);
$('#notes').html(el_html);
}, 2000);
<script src="https://cdnjs.cloudflare/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare/ajax/libs/handlebars.js/2.0.0/handlebars.js"></script>
<div class="col-12 col-md-3 order-md-1">
                        <div id="notes">
                            {{#each notes}}
                                {{this}}
                            {{/each}}
                        </div>
                    </div>

更多推荐

如何从ajax成功传递数据并在把手模板中呈现?

本文发布于:2024-05-07 21:19:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1757305.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并在   把手   模板   数据   ajax

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!