title: request.body is undefined in node post request
body: <p>I am trying to handle post ajax request at node server. My problem is, I am getting request body 'undefined'. kindly help me to understand if I am doing wrong. Following is the code I am using.</p>
  
  <ul>
  <li>Ajax request code:</li>
  </ul>
  
  <p>function sendRequest() {</p>
  
  <pre><code>    let http = new XMLHttpRequest();
      let url = 'http://&lt;hostname&gt;:3000/checkRequest';    
      let data = {};
      data.name = "nameString";
      data.age = "ageString";        
      http.open('POST', url, true);        
      //Send the proper header information along with the request
      http.setRequestHeader('Content-type','application/x-www-form-urlencoded');
      http.send(JSON.stringify(data));   
      http.onreadystatechange = function() {//Call a function when the state changes.
          if(http.readyState === 4 &amp;&amp; http.status === 200) {
              alert(http.responseText);
          }
      };   
  }
  </code></pre>
  
  <ul>
  <li><p>Request handler code:</p>
  
  <pre><code>// create express app
  const app = express();
  
  // parse application/x-www-form-urlencoded
  app.use(bodyParser.urlencoded({ extended: true }));
  
  // parse application/json
  app.use(bodyParser.json());
  
  // middleware route that all requests pass through
  app.use((request, response, next) =&gt; {
      console.log('Server info: Request received');
  
      let origin = request.headers.origin;
  
      // only allow requests from trusted origins
      if (originWhitelist.indexOf(origin) &gt; -1) {
          response.header('Access-Control-Allow-Origin', origin);
      }
  
      // only allow get and post requests, separate methods by comma e.g. 'GET, POST'
      response.header('Access-Control-Allow-Methods', 'GET, POST');
      response.header('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
      response.header('Access-Control-Allow-Credentials', true);
  
      // push through to the proper route
      next();
  });
  
  app.post('/checkRequest', (request, response) =&gt; {
           console.log(request.body.name) //undefined
  }
  </code></pre></li>
  </ul>