2017-08-04 78 views
1

我想從API獲取數據後呈現文件。如何從jQuery渲染ejs?

代碼

$(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 %> 

我怎樣才能解決這個問題?

回答

0
$(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 ...}); 
0

不能渲染你收到什麼用Ajax調用的數據EJS文件。 ejs在服務器端呈現,其中ajax調用發生在客戶端。 要解決您的問題,您需要在服務器端獲取用戶數據並將其發送到ejs文件。