节点EJS软件包随附位于
./node_modules/ejs/ejs.js或中的客户端javascript库
./node_modules/ejs/ejs.min.js。将其包含在页面上之后,您将需要加载模板,然后从模板生成HTML。
检测未定义的对象属性(在页面加载时加载模板会更理想):
function getData() { // Grab the template $.get('/results.ejs', function (template) { // Compile the EJS template. var func = ejs.compile(template); // Grab the data $.get('/data', function (data) {// Generate the html from the given data.var html = func(data);$('#divResults').html(html); }); });}EJS:
<table> <tr> <th>ID</th> <th>Name</th> </tr> <% data.forEach(function (d) { %> <tr> <td><%- d.id %></td> <td><%- d.name %></td> </tr> <% }); %></table>Ajax特快专递:
app.get('/data', function (req, res) { res.send({ data: [ { id: 5, name: 'Bill' }, { id: 1, name: 'Bob' } ]});});


