title: Angularjs: $http response set-cookie is not work with CORS
body: <p>I am using angular and java building a small website.</p>
  
  <p>I try to write a auth system, but i meet some problems with cookie.</p>
  
  <p>My web app is run on localhost:8081 while my java servlet is on localhost:8888.
  In my java code: </p>
  
  <p><code>response.addHeader("Access-Control-Allow-Origin", "http://localhost:8081");
   response.addHeader("Access-Control-Allow-Headers", "X-Requested-With");
   response.addHeader("Access-Control-Allow-Credentials", "true");</code></p>
  
  <p>In my angular code:</p>
  
  <p><code>$httpProvider.defaults.withCredentials = true;
       $httpProvider.defaults.useXDomain = true;
       delete $httpProvider.defaults.headers.common['X-Requested-With'];</code></p>
  
  <p>actually I've tried reading 
  <a href="https://stackoverflow.com/questions/15026016/set-cookie-in-http-header-is-ignored-with-angularjs">Set-Cookie in HTTP header is ignored with AngularJS</a></p>
  
  <p><a href="https://stackoverflow.com/questions/19383311/angularjs-http-does-not-seem-to-understand-set-cookie-in-the-response">Angularjs $http does not seem to understand “Set-Cookie” in the response</a></p>
  
  <p><a href="http://$http%20response%20Set-Cookie%20not%20accessible" rel="nofollow noreferrer">$http response Set-Cookie not accessible</a></p>
  
  <p>but I still meet strange problems.</p>
  
  <p>My first login: you can find that the server reponse a JSESSIONID and a mobile cookie in set-cookies header.
  <img src="https://i.stack.imgur.com/TYhCJ.png" alt="first_login_picture"></p>
  
  <p>but I check the chrome developer tool and find the resource>cookies empty.
  <img src="https://i.stack.imgur.com/pKcOp.jpg" alt="console"></p>
  
  <p>after login, I send another request to the server to get some list:
  but the strange thing is chrome send the JSEESIONID which is send from sever to client on the first login but can't  find in the chrome developer tool. while chrome don't send the 'mobile' cookie which is created by my self.
  <img src="https://i.stack.imgur.com/Wpz8s.png" alt="after_login"></p>
  
  <p>I set my cookie in server with:</p>
  
  <pre><code>CookieUtils.setCookie(response, "mobile",String.valueOf(customer.getPhone()), 3600*24*7, "/","localhost");
  request.getSession().setAttribute("user", customer);
  request.getSession().setMaxInactiveInterval(3600*24);
  </code></pre>
  
  <p>what can i do if i want to get the cookie in $http response and set it, and when request other thing with this cookie like my 'mobile' cookie, because i want to do authentication this way.</p>
  
  <p>while, the same code working with firefox is just ok, but not work in chrome 43.0.2357.65 m ,wtf..</p>