来自http://docs.angularjs.org/api/ng。$ http,它说我们应该设置默认标头以包含令牌,因此我正在关注它。
我的代码是这样的
var myapp = angular.module('myapp', ['ngCookies', 'ui.bootstrap']). config(['$routeProvider', function($routeProvider, $httpProvider, $cookies){ $routeProvider. when('/', { templateUrl: '/partials/home.html', controller: HomeCtrl }). when('/game/:gameId/shortlist/create',{ templateUrl: '/partials/create-shortlist.html', controller: CreateShortlistCtrl }). otherwise({redirectTo: '/'}); }]); myapp.run(function($rootScope, $http, $cookies, $httpProvider){ $http.get('/api/get-current-user').success(function(data){ $rootScope.current_user = data; $rootScope.current_team = $rootScope.current_user.team; }); $http.get('/api/get-current-season').success(function(data){ $rootScope.current_season = data; }); $rootScope.csrf_token = $cookies.csrftoken; console.log($httpProvider.defaults.headers.common); //$httpProvider.defaults.headers.post['X-CSRFToken'] = $cookies.csrftoken; });
如您所见,我已经应用了多种方法,但是无法使用csrf令牌设置标头。我遇到的两个错误是
未捕获的错误:未知提供程序:$ httpProviderProvider <-$ httpProvider
我究竟做错了什么?
如果您使用AngularJS 1.1.3或更高版本,则可以使用xsrfHeaderName和xsrfCookieName:
xsrfHeaderName
xsrfCookieName
var myapp = angular.module('myapp', ['ngCookies', 'ui.bootstrap']). config(['$routeProvider', function($routeProvider, $httpProvider, $cookies){ $httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken'; $httpProvider.defaults.xsrfCookieName = 'csrftoken'; ...
请参阅文档1.1.3中的$ location。