2014-03-07 45 views
0

我遵循David Mosher出色的前端工作流教程,使用lineman.js,angularjs前端和laravel後端(http://www.youtube.com/watch?v=fSAgFxjFSqY)構建應用程序。當手動引導AngularJS應用程序時丟失常量

我手動引導應用程序,以便我可以從laravel後端獲取csrf標記,並在引導angularjs應用程序之前將其注入爲常量。

我bootstrap.js文件:

(function() { 
    var $injector = angular.injector(['ng']); 
    $injector.invoke(function($http, $rootScope) { 
     $rootScope.$apply(function() { 
     $http.get("/api/auth/csrf_token").then(function(response) { 
      angular.module("app").constant("CSRF_TOKEN", response.csrf_token); 
      angular.bootstrap(document, ['app']); 
     }); 
     }); 
    }); 
    })(); 

我正在從laravel的csrf_token我laravel routes.php文件的文件是這樣的:

Route::get('/api/auth/csrf_token', function() { 
    return Response::json(array('csrf_token' => csrf_token())); 
}); 

我可以在Chrome瀏覽器開發看,我從後端擷取CSRF令牌:

XHR finished loading: "http://localhost:8000/api/auth/csrf_token". 
Network/Preview (->csrf_token): csrf_token: "BsMDDJ8ZjESpvdBLWBLz5ORM5LNXOsl3gx5LBcj5" 

問題: 我在我的角度應用程序中找不到CSRF_TOKEN常量。

問: 有沒有在我的CSRF_TOKEN的定義錯誤常數還是有我的引導代碼的任何其他錯誤?

任何幫助將不勝感激。

+1

也許下面的鏈接可能有幫助嗎? https://github.com/nervgh/angular-file-upload/issues/40 || http://stackoverflow.com/a/20446415/2150286 我將在大約一個小時內以相同的設置開始一個新項目 - 希望這個問題很快會得到一些關注/解決。讓我們知道你是否有自己的進步。 –

回答

0

CSRF_TOKEN存儲在'data.csrf_token'中,而不是'csrf_token'。我換成

angular.module("app").constant("CSRF_TOKEN", response.csrf_token); 

angular.module("app").constant("CSRF_TOKEN", response.data.csrf_token); 
在bootstrap.js文件

。現在它可以工作。

相關問題