javascript - How to create a ajax POST with node JS? -


i not sure how use ajax post post jade page node js. if can provide example or tell me missing script have, please let me know.

this script file:

  //add friends     $('.addcontact').click(function() {     $.post('/addcontact',        {friendrequest: $(this).data('user')});      if($(this).html!=='contact requested') {         return $(this).html('contact requested');     }     });   

the url have post on app.js file is:

app.post('/addcontact', user.addcontactpost); 

i trying post true click event on button add contact , change contact requested if data in db shown true.

this jade file:

extends layout block content        div     legend search results     div#userresults     user in ufirstname          a(href='/user/#{user.id}')             p #{user.firstname} #{user.lastname}         button.addcontact add contact 

the route file this:

    exports.addcontactpost = function(req, res, err) {     user.findbyidandupdate(req.signedcookies.userid, {                 $push: {friendrequest: req.body.friendrequest}             }, function(err) {                 if(err) {                     console.log("post2");                     return console.log('error');                     //return res.render('addcontacterror', {title: 'weblio'});                   }                   else {                     console.log('postsuccess');                     //alert('contact added');                     res.json({response: true});                  }              }); }; 

if posting ajax request, expecting js on client-side response, , react response accordingly. if separate request page - rendering whole page - actual option.

but need response server , update front-end without reloading based on response, need response server on post request json. , on client-side, templating, use jquery or templating libraries on client side it.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -