问题描述
当前,在每次发布请求后,尝试将带有新项目(克隆项目)信息的ejs模板插入到主ejs页面中,以克隆出一个“项目”模板。但是我似乎找不到正确的方法来正确执行该方法。我已经按照文档中的说明使用了前端ejs脚本。
当前出现错误: resp未定义
项目显示是所有项目的ejs模板 “库存滚动列表”是包含所有项目的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)
},})
})
解决方法
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});