如何在Ajax调用中呈现EJS文件或EJS代码。下面是我想做的代码。在这里,单击按钮后,相应的按钮ID将发送到服务器,并在那里用于在mongodb中搜索数据。我期望的数据正在获取,但不知道如何呈现。看一下代码:
view_id = $(this).attr('id');
alert(view_id);
var data = {"id":view_id};
$.ajax({
type: 'POST',
data: JSON.stringify(data),
contentType: "application/json",
dataType:'json',
url: '/transaction',
success: function(data) {
//render template here??
alert(JSON.stringify(data));
},
error: function(error) {
alert(JSON.stringify(error));
}
});
$('#dataModal').modal('show');
})
我正在从node.js方法获取数据
router.post('/transaction', function(req,res){
console.log('got request');
var id = req.body.id;
console.log(id);
transaction.find({"_id": id }, function(err, result){
if(err) console.log(err);
res.json(result);
}); });
这是我的具有ejs的html模态。
<div id="dataModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Complete Details</h4>
</div>
<div class="modal-body" id="view_detail">
<table id="datatable" class="table table-striped table-bordered bulk_action">
<thead>
<tr>
<th>Details</th>
<th>Data</th>
</tr>
</thead>
<tbody>
<tr>
<% for(i=0;i<data.length;i++){%>
<td><%= data[i].name %></td>
<td><%= data[i].phone %></td>
</tr>
<% } %>
</tbody>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
任何答案或建议都值得赞赏 ..谢谢你