简体   繁体   中英

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. However when I console.log(rendered) in the backend I'm getting undefined, and I'm also getting the error "Callback was already called." Specifically I'm using Express on the backend with the frontend views built in dust which is a similar to Jade etc.

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.

You can access html using this snippet:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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