Ajax Express NodeJS在页面的一部分加载ejs模板
好的,所以我有代码,把我带到我的索引页,然后我通过ajax调用代码给我回从我的mongoose数据库的结果。 所有这些调用正在工作,但我似乎无法弄清楚如何让我的代码执行成功的ajax调用加载另一个较小的ejs文件在当前页面的一部分? 我已经阅读了一些说我需要渲染ejs服务器端的东西,但是我怎样才能打电话给服务器,让它只更新页面的一部分,而不是整个页面加载。 我试图在已经产生完整结果的页面上执行searchfunction,所以我想用已经search的内容来replace完整的结果。 这里是我对数据库的导出调用:
exports.index = function(req, res, next){ Recipe.find(). sort('-updated_at'). exec(function(err, recipes, count){ if( err ) return next( err ); res.render('index', { title: 'Recipe Finder Index', recipes:recipes }); }) }; exports.search = function(req, res, next){ var param=req.params.search; console.log(param); Recipe.find({"fn":new RegExp(param)}). sort('-updated_at'). exec(function(err, recipes, count){ if( err ) return next( err ); //res.render('index', { title: 'Recipe Finder Index', recipes:recipes }); res.contentType('json'); console.log(JSON.stringify({recipes:recipes})); res.write(JSON.stringify({recipes:recipes})); res.end(); }) };
所以,然后我在加载到脚本标记中的第一个ejs文件中:
$('#submitSearch').click(function(){ console.log("clicked"); $.ajax({ url: '/ajax/'+$('input[name=search]').val() , type: 'POST' , cache: false , complete: function() { //called when complete console.log('process complete'); }, success: function(data) { console.log(data); console.log('process sucess'); //returns correct data, but then how can I call ejs? }, });
所以代码给了我正在寻找的对象,它从数据库中抓取数据,但是我在定位代码时遇到了问题,我的站点的一部分会重新加载一个带有新对象的ejs文件。 我在网上看到部分已不再支持,我应该使用包括,但看起来它只是给网站的一部分ejs,不告诉我如何使用ajax的新数据呈现ejs?
- Node.js + express.js + passport.js:在服务器重启之间保持身份validation
- Express无法读取PUT请求中req.data的属性值
- 如何在不使用req和res.locals的情况下实现NodeJS中的ThreadLocalvariablesfunction?
- 节点js,Express和句柄渲染fullcalendar插件无法正常工作
- 使用mustache.js部分快递
- node.js / express js模型和控制器之间的交互
- NodeJS项目成功部署到Heroku,但应用程序崩溃
- TypeError:请求path包含非转义字符
- 如何使用swagger-express-mw上传文件并expression?