【发布时间】:2016-09-29 19:01:00
【问题描述】:
我正在开发一个 Django-Angular 应用程序,并且需要使 csrf 令牌出现在所有 Angular AJAX 发布或放置请求中。我正在使用 ngCookies,但是当我尝试将 cookie csrf 令牌分配给所有有角度的帖子标题时,它给了我一个错误:
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
【问题讨论】:
标签: angularjs django post csrf