【问题标题】:Rendering a ejs file clients side渲染 ejs 文件客户端
【发布时间】:2020-08-16 04:58:48
【问题描述】:

目前正在尝试制定一些关于克隆“项目”模板的 ajax,在每次发布请求后,我试图将带有新项目(克隆项目)信息的 ejs 模板插入到主 ejs 页面中。但我似乎找不到正确的方法来正确执行它。我已经按照文档进行了前端 ejs 脚本。

当前出现错误: resp 没有定义

项目显示是所有项目的ejs模板 'inventory-scroll-list' 是包含所有项目的 div resp 是新创建/克隆的项目(猫鼬)

// cloning item with ajax (need to display new item)
$('.cloneItemButton').click(function () {
    const itemId = $(this).attr('id')
    $.ajax({
        method: 'POST',
        url: '/item/' + itemId + "/clone",
        success: function (resp) {
            console.log('Item Cloned: ' + resp._id)
            var html = ejs.render('<%- include("/itemDisplay",  { item: resp }); %>')

            $('.inventory-scroll-list').append(html)
        },
    })
})

【问题讨论】:

    标签: javascript express ejs


    【解决方案1】:
    var html = ejs.render('<%- include("/itemDisplay",  { item: resp }); %>')
    

    这里的 resp 是一个字符串,因为浏览器无法访问 /itemDisplay,所以它不起作用。

    如果ejs文件很小并且不依赖其他ejs文件,那么你可以直接将该文件的内容存储在一个变量中

    template = 'The entire contents of the ejs file';
    var html = ejs.render(template,  { item: resp});
    

    【讨论】:

    • 我得到一个exports.fileloader不是一个函数...
    • 未捕获的类型错误:ejs:1 >> 1| exports.fileLoader 不是函数
    • 我认为问题是浏览器无法访问 /itemDisplay
    • 您可以将 itemDisplay.ejs 的内容作为字符串存储在一个变量中,并使用它而不是使用包含函数
    猜你喜欢
    • 2017-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-11
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    相关资源
    最近更新 更多