栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Ajax调用Express Server之后使用jquery在ejs上生成内容

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Ajax调用Express Server之后使用jquery在ejs上生成内容

节点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' }    ]});});


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/372299.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号