0

I was fiddling with ExpressJS last night and found that given a simple code:

app.post('/contact', function(req, res, next) {
  res.send('Congrats you have submitted the form');
});

I am able to respond to it correctly from the server side and actually send a blank document with the text "Congrats you have submitted the form".

However when I remove the "action" attribute and try to handle it with a javascript to perform a POST request like for example using the axios module it doesn't work:

axios.post('/contact', { data: { username: <user entered data>, password: { <user entered password> }});

I am not able to grab that data from the server side (inside express)

app.post('/contact', function(req, res, next) {
  const username = res.data.username;
  const password = res.data.password;
  res.send('Login details: ' + username + ' / ' + password);
});
lorenzo-s
  • 16,603
  • 15
  • 54
  • 86
Chris
  • 973
  • 1
  • 8
  • 20

1 Answers1

1

Your axios code shoulod look like this:

axios.post('/contact', {username: 'user', password: 'pass'}).then(response => {
  console.log(response);
});

As for express code, you can find a good explanation in this answer: https://stackoverflow.com/a/12008719/3877000

Radu Nemerenco
  • 676
  • 4
  • 12