获取JSESSIONID值并在AngularJS中创建Cookie

Get JSESSIONID value and create Cookie in AngularJS

本文关键字:创建 Cookie AngularJS JSESSIONID 获取      更新时间:2023-09-26

我在AngularJS中使用客户端的Web RestFUL API。

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');
        });
     };
   },
]);

然后,我有这个Http报头响应

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

我正在使用ngCookies来获取JSESSIONID值,然后在我的浏览器中手动创建它,但我无法访问它。

我已经在StackOverflow上看到了所有关于这个的帖子,但是它们被弃用或完全不清楚。

谢谢你的关心和帮助。

你不能用JavaScript获得HttpOnly cookie。这就是HttpOnly标志的全部目的。

也就是说,你不需要这样做。cookie应该与任何请求一起自动发送到同一服务器。如果您正在处理跨域XHR请求,请将your_XHR_instance.withCredentials设置为true以包含cookie。