2017-08-04 11 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 %> 

どのように私はこの問題を解決することができますか?

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

答えて

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テンプレートをレンダリングしている前提としています。 ejsはサーバー側でレンダリングします。クライアント側ではajax呼び出しが行われます。 問題を解決するには、サーバー側でユーザーデータを取得してejsファイルに送信する必要があります。

0

は、あなたはAJAX呼び出しを受け取るどのようなデータを持つEJSファイルをレンダリングすることはできません。

関連する問題