【发布时间】:2020-03-09 14:36:25
【问题描述】:
目标: 我有一个按钮,它运行一个函数来从我的 Mongoose 数据库加载更多项目并将它们添加到表行中。我使用 get 从我的服务器端获取和返回数据。并且正在遵循this post 的指示,但我仍然无法呈现我需要的内容。
客户端代码:
<main role="main">
<div class="container-fluid ">
<div class="card-columns" id="test" value="<%=deals%>">
<tbody>
<tr id="content"><% include partials/row.html %></tr>
</tbody>
</div>
</div>
<div class="container" style="max-width: 25rem; text-align: center;">
<a id="8-reload" class="btn more" onclick="loadMore(8)"></a>
</div>
<script >
const loadMore = async function(x){
const response = await fetch(`/${x}`);
if (!response.ok)
throw oops;
const data =await response.text();
console.log(data);
console.log($("#content"));
await document.getElementById('content').insertAdjacentHTML('beforeend',data);
}
</script>
服务器 JS 请求:
app.get(`/:count`, (req, res) => {
const count = parseInt(req.params.count);
database.find({}, (err, found) => {
if (!err){
res.render("more",{items:found},(err,html)=>{
if(!err){
res.send(html);
}else{
console.log(err);
}
});
} else {
console.log(err);
}
}).skip(count).limit(25);
});
运行该函数时没有任何反应,浏览器控制台日志会读取长字符串 html。这对于我要附加到的地方:
jQuery.fn.init {} 原型:对象(0)
服务器控制台日志上没有错误。我错过了什么?
更新我尝试了附加而不是内容来测试,你瞧,我正在附加 html,而不是我的所有内容。 int 只插入整个 html 模板的打开和关闭,它之间没有任何内容。诡异的。
【问题讨论】:
-
您的响应 HTML 中有什么内容?如果它不是
<td>元素的集合,您将无法在<tr>中渲染它 -
做到了!非常感谢!
标签: javascript express append ejs fetch-api