我正在尝试构建一个角度+ laravel休息应用程序.我可以获得我的数据库的视图.当我尝试添加新项目时.我500 error告诉我错配csrf令牌.我的表单布局是:
<form class="form-horizontal" ng-submit="addItem()">
<input type="text" ng-model="itemEntry" placeholder="Type and hit Enter to add item">
</form>
Run Code Online (Sandbox Code Playgroud)
这是我尝试将项添加到数据库的方式:
$scope.addItem = function(CSRF_TOKEN) {
$http.post('/shop', { text: $scope.itemEntry, csrf_token: CSRF_TOKEN} ).success(function(data, status) {
if(data) {
var last = _.last($scope.items);
_token = CSRF_TOKEN;
$scope.items.push({text: $scope.itemEntry, bought: false, id: (last.id + 1) });
$scope.itemEntry = '';
console.log($scope.items);
} else {
console.log('There was a problem. Status: ' + status + '; Data: ' + data);
}
}).error(function(data, status) {
console.log('status: ' + status); …Run Code Online (Sandbox Code Playgroud) 我在Laravel BaseController中有以下代码.我想Authorization用带有令牌的标头来保护我的所有api资源.
public function __construct()
{
$this->beforeFilter('@getUserFromToken');
}
public function getUserFromToken($route, $request)
{
$accessToken = Request::header('Authorization');
if(!empty($accessToken)){
$this->currentUser = User::findByToken($accessToken);
}else{
return Request::header('Authorization'); //THE PROBLEM
return Response::json(['error'=>'Not authorized. Access token needed in Header.Authorization'], 403);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的.htaccess,如果这是相关的.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Run Code Online (Sandbox Code Playgroud)
因此,如果我有标记的问题行,Apache将完美地读取所有内容.我会得到我的回复,而不是得到403.但是,如果我没有该行,我将收到403自定义错误消息的错误.为什么?显然我使用相同的代码$this->currentUser = User::findByToken($accessToken);,为什么通过留下标记线我将能够得到标题?是否在场景后面发生重定向, …