title: NodeJS: how to perform Ajax Post with FormData
body: <p>I am testing a local HTML Form sending data to an aspx application as backend. Since I have some problem with CORS (even on localhost) I am trying to emulate the Ajax request performed by jQuery with NodeJS. I don't know if this is the right way to do. In the HTML form, after the jQuery validation, this is what I do:</p>
  
  <pre><code>submitHandler: function(form) {
      $.ajax({
          url: form.action,
          type: form.method,
          data: $(form).serialize(),
          success: function(response) {
              console.log(response);
          }            
      });
      //console.log($(form).serialize())
  }
  </code></pre>
  
  <p>and it works, until CORS ends the request. I mean that I can retrieve the data from the backend application.
  Instead, if I do:</p>
  
  <pre><code>function loadDoc() {    
  
    var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
    var xhttp = new XMLHttpRequest();
  
    /*var FormData = require('form-data');
    var myform = new FormData();
    myform.append('firstname', 'foo');*/
  
    xhttp.onreadystatechange = function() {
     if (this.readyState == 4 &amp;&amp; this.status == 200) {
      console.log(this.responseText);
     }
    };
    xhttp.open("POST", "http://127.0.0.1:1308/ImperoOrdini/ImperoOrdini.aspx?CMD=NUOVOORDINE", true);
  
    //which is the same string I get from .serialize() in jQuery
    xhttp.send("firstname=foo&amp;email=some@domain.it");
  }
  
  loadDoc();
  </code></pre>
  
  <p>I cannot get anything from the server application. If I want to get the parameter <code>firstname</code> from the POST data, I get <code>null</code>. So, where am I wrong?</p>
  
  <p><strong>UPDATE</strong>
  <a href="https://stackoverflow.com/questions/9768192/sending-data-through-post-request-from-a-node-js-server-to-a-node-js-server">This</a> is the only workaround I have found useful  in NodeJS:</p>
  
  <pre><code>var http = require('http');
  
  var querystring = require('querystring');
  var post_data = querystring.stringify({'firstname':'Lory'});
  
  var post_options = {
    host: 'localhost',
    port: '1308',
    path: '/ImperoOrdini/ImperoOrdini.aspx?CMD=NUOVOORDINE',
    method: 'POST',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Content-Length': Buffer.byteLength(post_data)
    }
  };
  
  // Set up the request
  var post_req = http.request(post_options, function(res) {
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
        console.log('Response: ' + chunk);
    });
  });
  
  // post the data
  post_req.write(post_data);
  post_req.end();
  </code></pre>
  
  <p>I had also tried with:</p>
  
  <pre><code>var request = require('ajax-request');
  request.post({
    url: 'http://127.0.0.1:1308/ImperoOrdini/ImperoOrdini.aspx?CMD=NUOVOORDINE',
    data: {'firstname':'Lory'},
    headers: {}
    }, function(err, res, body) {
       console.log(res);
     }
  );
  </code></pre>
  
  <p>but it did not work too. I feel such an ignorant and I would like to know the differences between those 3 libraries. 
  I have some doubts concerning the fact I must use <code>querystring.stringify()</code> in the working solution, because POST data are not in the URL and should not be uder the limits of query string, if I remember well.</p>