获取JSESSIONID值并在AngularJS中创建Cookie [英] Get JSESSIONID value and create Cookie in AngularJS

查看:258
本文介绍了获取JSESSIONID值并在AngularJS中创建Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using a Web RestFUL API from 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');
        });
     };
   },
]);

然后,我有这个Http标题响应

Then, i have this Http headers Response

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

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

I'm using ngCookies to get the JSESSIONID value and then create it manually in my browser, but i can't access it.

我已经在StackOverflow中看到了有关此内容的所有帖子,但它们已被弃用或完全不清楚。

I already saw all the posts about this in StackOverflow, but they are deprecated or completely unclear.

感谢您的关注和帮助。

推荐答案

您无法使用JavaScript获取HttpOnly cookie。这就是HttpOnly旗帜的全部目的。

You can't get HttpOnly cookies with JavaScript. That is the whole purpose of the HttpOnly flag.

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

That said, you shouldn't need to. The cookie 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 include cookies.

这篇关于获取JSESSIONID值并在AngularJS中创建Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆