title: AngularJS withCredentials
body: <p>I've been working on an AngularJS project which has to send AJAX calls to an restfull webservice. This webservice is on another domain so I had to enable cors on the server. I did this by setting these headers:</p>
  
  <pre><code>cresp.getHttpHeaders().putSingle("Access-Control-Allow-Origin", "http://localhost:8000");
  cresp.getHttpHeaders().putSingle("Access-Control-Allow-Credentials", "true");
  cresp.getHttpHeaders().putSingle("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
  cresp.getHttpHeaders().putSingle("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With");
  </code></pre>
  
  <p>I'm able to send AJAX requests from AngularJS to the backend but I'm facing a problem when I try to get an attribute of a session. I believe this is because the sessionid cookie doesn't get send to the backend. </p>
  
  <p>I was able to fix this in jQuery by setting withCredentials to true.</p>
  
  <pre><code>$("#login").click(function() {
      $.ajax({
          url: "http://localhost:8080/api/login",
          data : '{"identifier" : "admin", "password" : "admin"}',
          contentType : 'application/json',
          type : 'POST',
          xhrFields: {
              withCredentials: true
          },
          success: function(data) {
              console.log(data);
          },
          error: function(data) {
              console.log(data);
          }
      })
  });
  
  $("#check").click(function() {
      $.ajax({
          url: "http://localhost:8080/api/ping",
          method: "GET",
          xhrFields: {
              withCredentials: true
          },
          success: function(data) {
              console.log(data);
          }
      })
  });
  </code></pre>
  
  <p>The problem that I'm facing is that I can't get this to work in AngularJS with the $http service. I tried it like this:</p>
  
  <pre><code>$http.post("http://localhost:8080/api/login", $scope.credentials, {withCredentials : true}).
              success(function(data) {
                  $location.path('/');
                  console.log(data);
              }).
              error(function(data, error) {
                  console.log(error);
              });
  </code></pre>
  
  <p>Can anyone tell me what I'm doing wrong?</p>