Monday 10 April 2017

Pass ajax value to a variable in node.js server

I have an ajax get request that has a data of an input. I want to pass this data into a variable and use it in my query.

index.handlebars

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <title></title>
      </head>
      <body>
        <input type="hidden" class="test" name="test" value="sample"/>
       <script type="text/javascript">


    $( document ).ready(function() {
        var username = $('.test').val();
        $.ajax({
        type : 'GET',
        url: '/users//welcome',
        data : {
         wew: username
        }
        });
        alert(username); //correct output
      });
    </script>
          </body>
        </html>

and in my users/welcome

router.get('/welcome', ensureAuthenticated, function(req, res){

  var test = req.query.wew; //data from ajax request

  Item.find({username: test},function(err, docs, test){    //where username is the data
                console.log(test);
                res.render('welcome', {docs:docs});
        });
});

This code is not working. please help :/



via Lawrence Ricafort Bacus

No comments:

Post a Comment