title: Solving CORS when posting in angularJs using $HTTP with credentials
body: <p>On click of the submit button, the emailId and password is parsed and sent through the $http method.</p>
  
  <p>The Server is refusing to accept the Api call as the server and client have different IP and port.</p>
  
  <p>The server response header has the java CORS filter enabled.</p>
  
  <p>The server is running on STS and hosted on Apache Tomcat, the front-end is hosted on web storm server.</p>
  
  <p>DB is connected through @requestbody maps in java controller.</p>
  
  <p>I've included all possible combination of headers on client-side possible, but it hasn't helped.</p>
  
  <p>I tried with content type : 'application/x-www-form-urlencoded' aswell as text/html and application/json. Nothing seems to work.</p>
  
  <p>The error that the server shoots up is : "XMLHttpRequest cannot load <a href="http://182.72.xxx.xxx:9090/incite-merchant/connects/login" rel="nofollow">http://182.72.xxx.xxx:9090/incite-merchant/connects/login</a>. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '<a href="http://192.168.x.xxx:8080" rel="nofollow">http://192.168.x.xxx:8080</a>' is therefore not allowed access."</p>
  
  <p>Looking forword for help.</p>
  
  <pre><code>index.html
  
  &lt;div data-ng-app = "inciteapp" data-ng-init = "name= 'cafe'" container = "pizzaContainer"&gt;
  &lt;div ng-controller="pizzaController" class="email" data-ng-submit = "userInfo()" data-ng-model="name" style="width:24em" autoscroll&gt;
  
  &lt;p style="font-size:18px"&gt;Login from your mail.&amp;nbsp; OR &amp;nbsp;&amp;nbsp;&lt;a href="#2" data-ng-click="signuplink()"&gt;Sign-up now&lt;/a&gt;&lt;/p&gt;
  
  &lt;form class="form-horizontal form-group" form-submit = "submit()" id="myform" role="form" action="/server" name="loginform" novalidate&gt;
  
  &lt;div&gt;EmailId &lt;span style="color:red"&gt;*&lt;/span&gt;:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;input type="email" class="form-control-sm" data-ng-model="mail" id="userid" name="email"  placeholder="Enter EmailId" required="" autofocus/&gt;&lt;/div&gt;
  &lt;span style="color:red; font-size:14px" ng-show="loginform.email.$dirty &amp;&amp; loginform.email.$invalid" ng-show="noid"&gt;
  
  &lt;/span&gt;&lt;/br&gt;
  
  &lt;div&gt;Password&lt;span style="color:red"&gt;*&lt;/span&gt;: &amp;nbsp;&lt;input type="password" class = "form-control-sm" data-ng-model="pwd" id="password" name="pass" placeholder="Enter Password" required=""/&gt;&lt;/div&gt;
  &lt;/br&gt;
  
  &lt;input type="button" ng-click="logincheck()"  value="Submit" class="btn btn-xs btn-primary"&gt;
  
  &lt;/form&gt;
  &lt;/div&gt;
  
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  
  
  
  
  ctrl.js
  
  inciteapp.controller("pizzaController", ['$scope', '$http', function ($scope, $http)
  {
              'use strict';
  
              var vm = this;
  
              $scope.brand = {name: ""};
              $http.defaults.useXDomain = true;
              $scope.logincheck = function () {
              var myRequest = new XMLHttpRequest();
                  $.param = JSON.stringify({
                  "emailId":$scope.mail,
                  "brandName":$scope.name,
                  "password":$scope.pwd
  
              });
                  var user = JSON.parse($.param);
  
              $http({
                  url:'http://182.xx.xxx.xxx:9090/incite-merchant/connects/login',
                  method: 'POST',
                  data: user,
                  withCredentials : true,
                  /*xhr.credentials = crossdomain;*/
                  headers: {
                      'Content-Type': 'application/x-www-form-urlencoded',
                      'Access-Control-Allow-Origin': '*' | 'http://192.xxx.x.xxx:8080',
                      'Access-Control-Allow-Credentials': 'true',
                      'Access-Control-Allow-Headers': 'Origin, Authorization, Content-Type, X-Requested-With, Timeout, X-CSRF-Token, Accept, Accept-Version, Content-Length, Content-MD5,',
                      'Access-Control-Allow-Methods': 'GET, POST, PUT, OPTIONS',
                      'Access-Control-Expose-Headers': 'DAV, content-length, Allow',
                      'Access-Control-Max-Age': '3600'
  
                  }
              }).success(function (response, headers, config, status) {
                  console.log(user);
                  console.log(response);
  
                      if($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
                          header( "HTTP/1.1 200 OK" );
                          exit();
                      }*/
  
                  if(response.response === success)
                  {
                      window.location.href = "../11.06.15 Email login/welcome.html";
                  }
              }).error(function (response, headers, config, status) {
                  if(response == 200)
                  {
                      window.location.href = "../11.06.15 Email login/welcome.html";
                  }
                  if(response === null)
                  {
                      alert("Configure the server response!");
                  }
                  console.log(user);
              })
          }
  }]);
  </code></pre>