Saturday 18 March 2017

Get attribute from button to node using ajax

I'm simply trying to get the name of a clicked button to my node server and am stuck on something small. If I have something like this in node:

app.get('/someurl', function(req, res) {

    console.log(req.body.name);

});

And my jQuery/ajax looks something like this:

$(document).ready(function() {
  $('button').click(function() {
    buttonName(this.name);
  });
});

function buttonName(name) {
  $.ajax({
  type: 'get',
  url: '/someUrl',
  dataType: 'json',
  data: {'name' : name }
 })
}

What am I doing wrong? I'm using body-parser in node and have -

app.use(bodyParser.urlencoded({ extended: false }));



via Ja5onHoffman

No comments:

Post a Comment