Saturday 22 April 2017

node js how pass data to static html as a response to ajax call

. First of all i am newbie in node. I have read almost all similar posts but cannot figure out what is the best for my case. I make an ajax post request from a static html file sending a room name to join with. On server side i search my mongodb if there is a room with this particular name. If not, i want to return to the previous page sending also a message like "there isn't a room with this name". If i try res.send or res.json it redirects me to an empty page showing the message above. I do not prefer also making a url query. Is there a way to achieve this? Thanks

$('#btn2').click(function(){
            $.ajax({
                type: "POST",
                url: '/joinRoom',
                data: $('#roomJoin').val(),
                dataType: 'json',
                success: function (response) {
                    console.log(response);
                }

            });
        })

app.js

app.post('/joinRoom', checkAuthentication,function(req,res){
    console.log(req.body);
    User.findOne({ createRoom:req.body.roomJoin }, function(err,obj){
        if(err){
            console.log(err.code);
        }
        else if(!obj) {           
            res.redirect('index_connected.html');

        } else if(obj){
            res.redirect('board.html');
        }
    });

})



via nick314

No comments:

Post a Comment