Get the JSESSIONID value and create a cookie in AngularJS

I am using the Web RestFUL API for my client in AngularJS.

app.controller('LoginController', [ '$http', '$cookies', function($http, $cookies) { this.credentials = {}; this.http = $http; this.login = function() { console.log(this.credentials); var authdata = btoa( this.credentials.username + ':' + this.credentials.password ); $http.defaults.headers.common['Authorization'] = 'Basic ' + authdata; console.log($http); var res = $http.post("http://API_NAME/library"); res.success(function(data, status, header){ alert('Successfull func'); console.log($cookies.get('JSESSIONID')); }); res.error(function(data, status, headers, config) { console.log('Error Log'); }); }; }, ]); 

Then I have this Http headers answer

 Set-Cookie:JSESSIONID=azekazEXAMPLErezrzez; Path=/; HttpOnly 

I use ngCookies to get the JSESSIONID value and then create it manually in my browser, but I cannot access it.

I have already seen all the posts about this in StackOverflow, but they are deprecated or completely obscure.

Thank you for your interest and help.

+6
source share
1 answer

You cannot receive HttpOnly cookies using JavaScript. This is the purpose of the HttpOnly flag.

However, you do not need to. Cookies should be sent automatically with any request to the same server. If you are dealing with cross-origin XHR requests, set your_XHR_instance.withCredentials to true to enable cookies.

+7
source

All Articles