Angular ngCookies $cookie 未定义
Angular ngCookies $cookie is undefined
我正在开发一个 Django-Angular 应用程序,需要使 csrf 令牌在所有 Angular AJAX post 中或发出请求。我正在使用 ngCookies,但是当我尝试将 cookies csrf 令牌分配给所有 angular post headers 时,它给我一个错误:
TypeError: Cannot read property 'csrftoken' of undefined
$cookieStore 也未定义,并且对 $cookies 使用 get 方法也不起作用。我正在为 angular-cookies 加载正确的脚本,它与我的 angular 版本相匹配,而且我没有收到有关 ngCookies 本身的任何错误,所以我不太确定它可能是什么?
(function () {
'use strict';
Config.$inject = ["$locationProvider", "$stateProvider", "$urlRouterProvider", "$httpProvider"];
angular
.module('app.guest', [
'app.common',
'app.core',
'app.repo',
'ui.bootstrap',
'ngCookies'
])
.config(Config);
.run(['$cookies'], function ($rootScope, $http, $cookies) {
// set the CSRF token here
$http.defaults.headers.post['X-CSRFToken'] = $cookies.csrftoken;
})
/** ngInject */
function Config($locationProvider, $stateProvider, $urlRouterProvider, $httpProvider, $cookies) {
broadcastReady.$inject = ["CommonService", "CommonEvents"];
$locationProvider.html5Mode(true);
$httpProvider.defaults.xsrfCookieName = 'csrftoken';
$httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken';
$httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
$httpProvider.defaults.headers.post['X-CSRFToken'] = $cookies.csrftoken;
控制台
vendor.js:12275 Uncaught Error: [$injector:modulerr] Failed to instantiate module app.guest due to:
TypeError: Cannot read property 'csrftoken' of undefined
在您的 $inject
列表末尾添加 "$cookies"
:
Config.$inject = ["$locationProvider", "$stateProvider", "$urlRouterProvider", "$httpProvider", "$cookies"];
确保你也加载了 angular-cookies.js
。
我正在开发一个 Django-Angular 应用程序,需要使 csrf 令牌在所有 Angular AJAX post 中或发出请求。我正在使用 ngCookies,但是当我尝试将 cookies csrf 令牌分配给所有 angular post headers 时,它给我一个错误:
TypeError: Cannot read property 'csrftoken' of undefined
$cookieStore 也未定义,并且对 $cookies 使用 get 方法也不起作用。我正在为 angular-cookies 加载正确的脚本,它与我的 angular 版本相匹配,而且我没有收到有关 ngCookies 本身的任何错误,所以我不太确定它可能是什么?
(function () {
'use strict';
Config.$inject = ["$locationProvider", "$stateProvider", "$urlRouterProvider", "$httpProvider"];
angular
.module('app.guest', [
'app.common',
'app.core',
'app.repo',
'ui.bootstrap',
'ngCookies'
])
.config(Config);
.run(['$cookies'], function ($rootScope, $http, $cookies) {
// set the CSRF token here
$http.defaults.headers.post['X-CSRFToken'] = $cookies.csrftoken;
})
/** ngInject */
function Config($locationProvider, $stateProvider, $urlRouterProvider, $httpProvider, $cookies) {
broadcastReady.$inject = ["CommonService", "CommonEvents"];
$locationProvider.html5Mode(true);
$httpProvider.defaults.xsrfCookieName = 'csrftoken';
$httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken';
$httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
$httpProvider.defaults.headers.post['X-CSRFToken'] = $cookies.csrftoken;
控制台
vendor.js:12275 Uncaught Error: [$injector:modulerr] Failed to instantiate module app.guest due to:
TypeError: Cannot read property 'csrftoken' of undefined
在您的 $inject
列表末尾添加 "$cookies"
:
Config.$inject = ["$locationProvider", "$stateProvider", "$urlRouterProvider", "$httpProvider", "$cookies"];
确保你也加载了 angular-cookies.js
。