title: XMLHttpRequest cannot load http://127.0.0.1:1337/. No 'Access-Control-Allow-Origin' header is present on the requested resource
body: <p>I try to send and receive data from HTML page with Angular.js and locally running Node.js server</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>app.controller("MainController", function MainController($scope, $http){  
   $scope.postIt = function() {
    //alert("posting...");
    $http.post('http://127.0.0.1:1337/', {msg:'hello from Angular.js!'})
    .success(function onSuccess(response){
           console.log(response);
           $scope.$apply(function(){
               $scope.testData = JSON.parse(response);
               console.log($scope.testData);
           });
       }).error(function onError(){
           console.log("AJAX failed!");
       });
      };
  }); </code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div id='content' 
       ng-app='MyTutorialApp' 
       ng-controller='MainController'&gt;
    &lt;p&gt;    
          &lt;a ng-click="postIt()"&gt;
              Click here to load data.
          &lt;/a&gt;
       &lt;/p&gt;
  &lt;/div&gt; 
   &lt;script src="bower_components/angular/angular.js" type="text/javascript"&gt;&lt;/script&gt;
   &lt;script src="app.js" type="text/javascript"&gt;&lt;/script&gt;
   &lt;script src="maincontroller.js" type="text/javascript"&gt;&lt;/script&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>then I get error:</p>
  
  <pre><code> XMLHttpRequest cannot load http://127.0.0.1:1337/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
  </code></pre>
  
  <p>I try to apply headers as <code>app.js</code></p>
  
  <pre><code>var app = angular.module('MyTutorialApp', [], function($httpProvider) {
        $httpProvider.defaults.headers.post['Access-Control-Allow-Origin'] = 'http://127.0.0.1:1337/';
  });
  </code></pre>
  
  <p>but that does not take effect.</p>
  
  <p>Node.js code</p>
  
  <pre><code>var http = require('http');
  http.createServer(function handler(req, res) {
      console.log(req.method, req.url, req.httpVersion, req.headers);
  
      res.writeHead(200, {'Content-Type': 'text/plain'});
      res.end('Hello from Node.js\n');
  }).listen(1337, '127.0.0.1');
  console.log('Server running at http://127.0.0.1:1337/');
  </code></pre>