ejs ajax node,node.js - Ajax Express NodeJS to load ejs template in part of page - Stack Overflow

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?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值