Ok, so I have code that brings me to my index page, then I am calling code through ajax to give me back results from my mongoose db. All of those calls are working, but I cannot seem to figure out how to get my code to execute on success of the ajax call to load another smaller ejs file in a portion of the current page? I have read a few things that say I need to render the ejs server side, but how to I make a call to the server and have it only update a part of the page and not do a whole page load. I am trying to execute a search function on a page that already yields the full results, so I want to replace the full results with what has been searched. Here are the export calls I have to the database:
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();
})
};
So then I have this inside the first ejs file that loads in a script tag:
$('#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?
},
});
So the code gives me the objects I am seeking, it grabs the data from the database, but I am having trouble locating code to have a portion of my site reload an ejs file with the new objects I have gotten back. I saw online that partials are no longer supported and I should be using include, but that looks like it only gives an ejs to a portion of the site, does not tell me how to render the ejs with the new data from ajax?