繁体   English   中英

如何从jQuery渲染ejs?

[英]How to render ejs from jQuery?

我想从API获取数据后呈现文件。

$(document).ready(function() {
   $(document).delegate( "#editUser", "click", function() {
        var userId = $(this).data('id');
        $.post("/userProfile",{"userId":userId},function(data, status){
        console.log(data); //gets data here
        //how to render ejs file here with the above data? 
    });

});

ejs文件: (sample.ejs)

<% include header.html %>
 <strong>Sample ejs</strong>
<ul>
 <li><%= data.name %> says: <%= data.message %></li>
</ul>
 <% include footer.html %>

我怎么解决这个问题?

$(document).ready(function() {
   $(document).delegate( "#editUser", "click", function() {
        var userId = $(this).data('id');
        $.post("/userProfile",{"userId":userId},function(html, status){
        $("#some-container").append(html);
    });
});

上面的代码假定您正在从服务器端渲染ejs模板,如下所示:

res.render('sample', {param1: param1 ...});

您无法使用通过ajax调用接收到的数据来呈现ejs文件。 ejs呈现在服务器端,而ajax调用发生在客户端。 为了解决您的问题,您需要自行获取服务器端的用户数据并将其发送到ejs文件。

暂无
暂无

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

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