title: Ajax post data from form to send to express js body parser
body: <p>I am tring to send Form data to node js via ajax,<br>
  I am using express with body parser on node js<br>
  But I am getting undefined in req.body</p>
  
  <p>I searched every in net and tried many things and nothing worked for me
  Kindly help me with the correct way of doing this.</p>
  
  <p>|*| Html Code :</p>
  
  <pre><code>&lt;html lang="en"&gt;
  &lt;head&gt;
      &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"&gt;&lt;/script&gt;
      &lt;title&gt;Add&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
      &lt;h1&gt;Welcome to Website &lt;/h1&gt;
      &lt;form id="addUserForm"&gt;
          &lt;h3&gt; Enter the your details : &lt;/h3&gt;
          Name :&lt;br&gt;&lt;input type="text" name="nameView"&gt;&lt;br&gt;&lt;br&gt;
          Email :&lt;br&gt;&lt;input type="email" name="mailView"&gt;&lt;br&gt;&lt;br&gt;
          Mobile :&lt;br&gt;&lt;input type="number" name="mobileView"&gt;&lt;br&gt;&lt;br&gt;
          &lt;input type="submit" value="Submit"&gt;
      &lt;/form&gt;
  </code></pre>
  
  <p>|*| Ajax Code :</p>
  
  <pre><code>    &lt;script&gt;
          $(document).ready( function()
          {
              $('#addUserForm').submit(function()
              {
                  var formDataVar = new FormData($(this)[0]);
  
                  console.log(formDataVar);
                  $.ajax({
                      url: '/addUserFormSubmit',
                      type:'POST',
                      data: formDataVar,
                      dataType: 'json',
                      contentType: false,
                      processData: false,
                  }).done(function(ResJryVar)
                  {
                      console.log(ResJryVar);
                  });
              })
          });
  
      &lt;/script&gt;
  &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p>|*| I also tried :</p>
  
  <pre><code>var formDataVar = new FormData();
  formDataVar.append( 'nameView', input.files[0] );
  formDataVar.append( 'mailView', input.files[1] );
  formDataVar.append( 'mobileView', input.files[2] );
  </code></pre>
  
  <p>and</p>
  
  <pre><code>var formDataVar = {};
  $.each($('#addUserForm').serializeArray(), function(i, field)
  {
      formDataVar[field.name] = field.value;
  });
  </code></pre>
  
  <hr>
  
  <p>|*| Node Js Code :</p>
  
  <pre><code>var express = require("express");
  var bodyParser = require("body-parser");
  var app = express();
  
  app.listen(8888,function()
  {
      console.log("Server Started and Running ...");
  });
  
  app.use(bodyParser.json());
  app.use(bodyParser.urlencoded({extended: true}));
  
  app.get('/',addUserFormFnc);
  app.post('/addUserFormSubmit',addUserSubmitFnc);
  
  function addUserFormFnc(req, res)
  {
      res.sendFile('addUser.html', {root: __dirname });
  }
  
  function addUserSubmitFnc(req, res)
  {
      console.log("Data Received : ");
      var userObjVar =
      {
          nameView: req.body.nameView,
          mailView: req.body.mailView,
          mobileView: req.body.mobileView
      };
      console.log(userObjVar);
      res.send(userObjVar);
  
  }
  </code></pre>
  
  <hr>