从angular度读取nodejs中的cookie
我正在使用angular和nodejs创build一个项目。 我的代码有问题。 我在nodejs中设置cookie,并想读取angular度的一面。 我不知道我怎么能做到这一点。
这里是设置cookie
cookies = {expire : new Date(new Date().getTime()+20*60*1000), maxAge : 600000, httpOnly : true, secure : true}; res.cookie('cookiename', "cookievalue", cookies);
在angularjs中:
console.log(document.cookie) //get blank value
请帮忙
你可以使用AngularJS API: https : //docs.angularjs.org/api/ngCookies/service/$cookies
angular.module('cookiesExample', ['ngCookies']) .controller('ExampleController', ['$cookies', function($cookies) { // Retrieving a cookie var favoriteCookie = $cookies.get('myFavorite'); // Setting a cookie $cookies.put('myFavorite', 'oatmeal'); }]);
读取值是使用$ cookie.get(key)方法的问题:
console.log( $cookie.get('cookiename') ); // cookie value
为了在请求到服务器之间持久化cookie,请在Angular中使用此configuration:
angular.config(function ($httpProvider) { $httpProvider.defaults.withCredentials = true; })
这将通过XHR调用将cookie数据发送回服务器到您的nodejs服务器。