无法理解如何使其工作...我试图从API获取CSRF并将其作为常量加载到我的angularJS应用程序中。 这是我在@David Mosher中使用的代码https://gist.github.com/davemo/6141699我通过这样做启动app.js:
// Retrieve and inject the CSRF token from the server (function() { var $injector = angular.injector(['ng']); $injector.invoke(function($http,$rootScope) { $rootScope.$apply(function() { $http.get("http://api.local/auth/csrf_token").then(function(response) { angular.module("app").constant("CSRF_TOKEN", response.data); console.log(CSRF_TOKEN); angular.bootstrap(document, ['app']); }); }); }); })();当我检查它时,返回带有csrf_token的200。 但是CSRF_TOKEN在应用程序中无处设置... console.log(CSRF_TOKEN)返回ReferenceError:CSRF_TOKEN未定义....
知道我做错了什么?
非常感谢! :-)
Can't understand how to make it work... I am trying to get the CSRF from the API and load it as a constant in my angularJS app. Here is the code I used from @David Mosher https://gist.github.com/davemo/6141699 I start the app.js by doing that:
// Retrieve and inject the CSRF token from the server (function() { var $injector = angular.injector(['ng']); $injector.invoke(function($http,$rootScope) { $rootScope.$apply(function() { $http.get("http://api.local/auth/csrf_token").then(function(response) { angular.module("app").constant("CSRF_TOKEN", response.data); console.log(CSRF_TOKEN); angular.bootstrap(document, ['app']); }); }); }); })();When I check it returns a 200 with the csrf_token. However the CSRF_TOKEN is set nowhere in the app... the console.log(CSRF_TOKEN) return ReferenceError: CSRF_TOKEN is not defined....
Any idea what I am doing wrong??
Thank you so much! :-)
最满意答案
我在laravel项目中也使用了角度。 这是我做的,它对我有用:
为了开始角度我使用这个(我需要这个[[与刀片{{语法。没有冲突):
var laravelApp = angular.module('laravelApp', ['ui.bootstrap', 'ngResource'], function($interpolateProvider) { $interpolateProvider.startSymbol('[['); $interpolateProvider.endSymbol(']]'); });在我的index.blade.php中,我将csrf标记定义为常量:
@section('javascripts') <script> angular.module("laravelApp").constant("CSRF_TOKEN", '{{ csrf_token() }}'); </script> @endsection在我的控制器中,我也在角度中使用这个CSRF_TOKEN常量:
laravelApp.controller('startCtrl', function($scope, $http, CSRF_TOKEN, $window) { // init $scope.init = function () { $scope.template_choose = 'start'; $scope.loadTemplateURL = '/template/load'; } // POST Request with csrf_token $scope.loadActualTemplate = function() { $http.post($scope.loadTemplateURL, { '_token' : CSRF_TOKEN }).then(function(Response) { $scope.template_choose = '/template/choose/'+Response.data; }); } // start $scope.init(); });在刀片模板中定义CSRF_TOKEN。 在控制器中交付它。 将它用作$ http.post中的Post参数。 这工作:)
Move the code shown above on top of your services.js if you have one
更多推荐
发布评论