从浏览器获取HTTP cookie

时间:2015-01-09 14:51:01

标签: angularjs cookies

我是用户AngularJS,我想阻止从服务器发送保存默认cookie,因为它的expirasion设置为session。 或者只是获取此cookie,获取身份验证值,并将其保存到我自己的cookie中,我将用它来验证请求。 但现在,我无法使用AngularJS或JavaScript获取此cookie。我只能看到“__ngdebug”或“5984_recent”。 “AuthSession”cookie被HTTP检查为true;

我尝试了来自$ http的监视标题,但是没有名为“Set-Cookie”的示例服务器应返回:

HTTP/1.1 200 OK
Cache-Control: must-revalidate
Content-Length: 43
Content-Type: application/json
Date: Mon, 03 Dec 2012 01:23:14 GMT
Server: CouchDB (Erlang/OTP)
Set-Cookie: AuthSession=cm9vdDo1MEJCRkYwMjq0LO0ylOIwShrgt8y-UkhI-c6BGw; Version=1; Path=/; HttpOnly

我的控制器看起来像那样:

ctr.controller("loginController", ["$scope", "$cookieStore", "$http",
  function($scope, $cookieStore, $http) {

    $scope.userCedential = {};

    $scope.login = function() {

        var headers = {};
        headers["Content-Type"] = "application/json";

        var res = $http.post("/_session", $scope.userCedential, headers);

        res.success(function(data, status, headers, config) {
                var s = headers("Set-Cookie");

            });

        res.error(function(data, status, headers, config) {

            });
    };

  }]);

1 个答案:

答案 0 :(得分:3)

你不能

根据定义,无法从NON-Http接口访问 HttpOnly cookie 。即JavaScript

http://en.wikipedia.org/wiki/HTTP_cookie#HttpOnly_cookie

另请参阅此SO答案以获取有关AJAX和HttpOnly如何交互的更多信息:

How do HttpOnly cookies work with AJAX requests?