【问题标题】:Appending fetched ejs items附加获取的 ejs 项目
【发布时间】: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 中有什么内容?如果它不是 &lt;td&gt; 元素的集合,您将无法在 &lt;tr&gt; 中渲染它
  • 做到了!非常感谢!

标签: javascript express append ejs fetch-api


【解决方案1】:

好吧,看来大问题是两件事。第一个问题是我要附加到内容无法附加到的地方。第二个问题是我的模板从表格行开始,其中包含更多内容,不允许其他内容呈现。一旦我将我的 jquery 移动到父 id 并删除表行,一切都很好!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-26
    • 2021-12-06
    • 1970-01-01
    • 2016-01-23
    • 2017-01-26
    • 1970-01-01
    相关资源
    最近更新 更多