$ http does not send cookies to Requests

We are working on a RESTful Webservice with AngularJS and Java Servlets. When a user logs in, our database sends the Set-Cookie header to the frontend. In Angular, we access the header through $cookies (ngCookie-module) and install it. enter image description here

Now, when the user logs in, he can, for example, delete some things. Therefore, the external interface sends a GET request to the server. Since we work in different domains, we need to set some CORS headers, and Angular executes an OPTIONS request before the actual GET request:

OPTIONS request: OPTIONS request

GET Request Get request

We do this in Angular through the $ http module, but it just won’t send a cookie containing JSESSIONID .

How can I enable Angular to send cookies?

+64
angularjs html5 webkit
Jun 12 '13 at 11:52
source share
2 answers

In your configuration, DI $httpProvider , and then set withCredentials to true:

 .config(function ($httpProvider) { $httpProvider.defaults.withCredentials = true; //rest of route code }) 

Information about angularjs withCredentials: http://docs.angularjs.org/api/ng.$http

What are the links to the mozilla article: https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS?redirectlocale=en-US&redirectslug=HTTP_access_control#section_5

+127
Jun 12 '13 at 12:30
source
 $http.get("URL", { withCredentials: true }) .success(function(data, status, headers, config) {}) .error(function(data, status, headers, config) {}); 

Another thing to keep in mind: you need to enable third-party cookies . If you have globally disabled in Chrome, click β€œi” -Symbol to the left of the domain name, then cookies, then β€œlock” and unlock the target domain.

+7
Nov 05 '15 at 22:07
source



All Articles