title: AJAX sending data to Node.js server
body: <p>I'm trying to send data with AJAX to a Node.js server but keep bumping into the same issue, the reception.</p>
  
  <p>Here is the client-side JavaScript / AJAX code :</p>
  
  <pre><code>    var objects = [
          function() { return new XMLHttpRequest() },
          function() { return new ActiveXObject("MSxml2.XMLHHTP") },
          function() { return new ActiveXObject("MSxml3.XMLHHTP") },
          function() { return new ActiveXObject("Microsoft.XMLHTTP") }
      ];
      function XHRobject() {
          var xhr = false;
          for(var i=0; i &lt; objects.length; i++) {
              try {
                  xhr = objects[i]();
              }
              catch(e) {
                  continue;
              }
              break;
          }
          return xhr;
      }
      var xhr = XHRobject();
      xhr.open("POST", "127.0.0.1:8080", true);
      xhr.setRequestHeader("Content-Type", "text/csv");
      xhr.send(myText);
      console.log(myText);
  </code></pre>
  
  <p>For some reason the basic HTTP server you can get on <a href="http://nodejs.org/api/http.html%20nodejs.org" rel="nofollow noreferrer">http://nodejs.org/api/http.html</a> was causing <code>ECONNREFUSED</code> errors (even with sudo and port 8080) so I tried with this simple code :</p>
  
  <pre><code>var http = require('http');
  
  http.createServer(function(req, res) {
      console.log('res: ' + JSON.stringify(res.body));
  }).listen(8080, null)
  </code></pre>
  
  <p>But it keeps printing <code>res: undefined</code>.</p>
  
  <p>On the other hand, the <code>AJAX</code> <code>POST</code> doesn't seem to trigger any errors in the console.</p>
  
  <p>So my question if the following :</p>
  
  <ul>
  <li>How can I have a simple but solid node.js server that can retrieve
  text that was send by an <code>AJAX</code> <code>POST</code> request ?</li>
  </ul>
  
  <p>Thank you in advance !</p>
  
  <p>Edit : The testing is done on <code>127.0.0.1</code> (localhost).</p>
  
  <p>EDIT2: This is the updated Node.js code :</p>
  
  <pre><code>var http = require('http');
  
      var http = require('http');
      http.createServer(function (req, res) {
        res.writeHead(200, {'Content-Type': 'text/plain'});
        res.end();
        req.on('data', function(data) {
          console.log(data);
        })
      }).listen(1337, '127.0.0.1');
      console.log('Server running at http://127.0.0.1:1337/');
  </code></pre>