ExpressJS change route without refresh

百般思念 提交于 2020-01-07 03:00:12

问题


Hey I have a website run with Express.js and Handlebars that has a navbar and sidebar that stays the same for all the pages. When I route it only changes the page container which is {{{body}}}.

Is there a way I can just load the content of the routes without the page actually refreshing and reloading the nav and sidebar?

I'm currently using this for routing.

router.get('/', checkAuthentication, function(req, res){
  res.render('index');
});

Any help would be nice as I'm quite new with node.

I want only the container to load not the whole url/page


回答1:


you can send the data you need to the client using:

res.send(JSON.stringify(Obj);

and in the ajax success function you need to:

JSON.parse(dataSentFromServer);

and render the data using handlebars in the success function or another function that you call




回答2:


Thank you all for considering/looking at the post, i have come to the conclusion to be using PJAX to do this.

The tutorial i read, for anyone wondering is : Pjax Tutorial



来源:https://stackoverflow.com/questions/44901025/expressjs-change-route-without-refresh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!