AngularJS Cookies无效

时间:2016-06-17 16:11:32

标签: angularjs cookies

代码有效,Cookie myCookie可在浏览器中访问(在Firefox中使用Firebug进行检查),但在角度代码中,我无法获取它。 AngularJS版本:1.5.7

.controller('MainCtrl', ['$scope', '$http', '$cookies', function ($scope, $http, $cookies) {
var dummy = {name: 'myuser', role: 'myrole'};
var authURL = 'http://mycustom.url';
$http({
    method: 'POST',
    url: authURL,
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    transformRequest: function (obj) {
        var str = [];
        for (var p in obj)
            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
        return str.join("&");
    },
    data: dummy
})
    .then(function successCallback(response) {
        var cook = $cookies.get('myCookie');
        console.log('Cookie: ' + cook); //this doesn't work.shows undefined
    }, function errorCallback(response) {
        console.log('ERROR');
    });

}]);

0 个答案:

没有答案