Skip to content
Advertisement

understanding body parameters in javascript

im having difficulty understanding how form data are taken in javascript. For example:

firstName: req.body.firstName || null,
                lastName: req.body.lastName || null

are firstName and lastName id’s from the html used to identify what field the data is coming from?

Thank you!

Advertisement

Answer

We can’t really tell based on what you posted, if it is a direct form post (and not AJAX), then the data will come from input/select elements with those respective names, e.g.

<form method="POST" action="/express/endpoint">
   <input type="text" name="firstName" />
   <input type="text" name="lastName" />
   <input type="submit" />
</form>

This can also be sent manually via AJAX:

fetch('/express/endpoint', {
    body: JSON.stringify({ firstName: 'foo', lastName: 'bar' }),
    headers: {
      'content-type': 'application/json'
    },
    method: 'POST'
}).then(function(response) {
  console.log(response)
})
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement