使用RESTFull API Laravel的Angular JS和CSRF(Angular JS & CSRF with RESTFull API Laravel)

系统教程 行业动态 更新时间:2024-06-14 17:04:02
使用RESTFull API Laravel的Angular JS和CSRF(Angular JS & CSRF with RESTFull API Laravel)

无法理解如何使其工作...我试图从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

更多推荐

本文发布于:2023-04-24 20:52:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/aa135d2b5e9c714a24e3d4149292b1cf.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Laravel   API   RESTFull   CSRF   JS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!