简体   繁体   English

通过AJAX渲染视图

[英]Rendering view via AJAX

I'm trying to do an infinite scroll ie, when a user gets to the bottom of a page more content loads. 我正在尝试进行无限滚动,即,当用户到达页面底部时,将加载更多内容。 I make an ajax call and then on the backend I'm trying to render the new view and return that view so then on the frontend I can append it to the existing DOM. 我进行了ajax调用,然后在后端尝试渲染新视图并返回该视图,因此在前端可以将其附加到现有DOM。 However when I console.log(rendered) in the backend I'm getting undefined, and I'm also getting the error "Callback was already called." 但是,当我在后端console.log(rendered)时,我变得不确定,并且还收到错误消息:“已调用回调。” Specifically I'm using Express on the backend with the frontend views built in dust which is a similar to Jade etc. 具体来说,我在后端使用Express,并且前端视图内置在灰尘中,这类似于Jade等。

router.get('/get_more_content', function(req, res) {
    ... (db parsing logic - this part works fine) ...
    rendered = res.render('galleries');
    return res.status(200).send({ rendered: rendered });
})

You are getting this error because res.render ends the connection unless you specify callback argument. 您会收到此错误,因为除非您指定回调参数,否则res.render终止连接。

You can access html using this snippet: 您可以使用以下代码段访问html:

res.render('galleries', function(err, html) {
  res.status(200).send({ rendered: html });
});

For more information please refer to http://expressjs.com/en/api.html#res.render 有关更多信息,请参阅http://expressjs.com/en/api.html#res.render

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM