Laravel 5 AngularJS POST不工作(TokenMismatchException)

时间:2016-02-06 17:31:13

标签: php angularjs laravel-5 laravel-5.1 csrf-protection

我正在使用Laravel 5开发网站,我使用AngularJS

这是我的代码

$scope.save = function(modalstate, id) {
    var url = API_URL + "tools";

    if (modalstate === 'edit'){
        url += "/" + id;
    }

    $http({
        method: 'POST',
        url: url,
        data: $.param($scope.tools),
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
    }).success(function(response) {
        console.log(response);
        location.reload();
    }).error(function(response) {
        console.log(response);
        alert(response);
        alert('Error');
    });
}

始终显示错误提醒。从日志中,一些错误包括TokenMismatchException和VerifyCsrfToken。似乎csrf令牌存在问题。

我的AngularJS文件位于视图(刀片)的不同目录中。我所做的是在视图文件中包含隐藏的输入。

<input id="_token" name="_token" type="hidden" value="{{ csrf_token() }}" ng-model="tools._token">

但我得到了同样的错误。如何解决此错误?

更新

PHP版本:7.0.2,Laravel:5.1.27

Laravel日志文件

#1 C:\xampp\htdocs\MyWeb\bootstrap\cache\compiled.php(9584): call_user_func_array(Array, Array)
#2 C:\xampp\htdocs\MyWeb\bootstrap\cache\compiled.php(12952): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#3 [internal function]: Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#4 C:\xampp\htdocs\MyWeb\bootstrap\cache\compiled.php(9584): call_user_func_array(Array, Array)
#5 C:\xampp\htdocs\MyWeb\bootstrap\cache\compiled.php(11549): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#6 [internal function]: Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#7 C:\xampp\htdocs\MyWeb\bootstrap\cache\compiled.php(9584): call_user_func_array(Array, Array)
#8 C:\xampp\htdocs\MyWeb\bootstrap\cache\compiled.php(12689): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#9 [internal function]: Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure))
#10 C:\xampp\htdocs\MyWeb\bootstrap\cache\compiled.php(9584): call_user_func_array(Array, Array)
#11 C:\xampp\htdocs\MyWeb\bootstrap\cache\compiled.php(12626): Illuminate\Pipeline\Pipeline-  >Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#12 [internal function]: Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request), Object(Closure))
#13 C:\xampp\htdocs\MyWeb\bootstrap\cache\compiled.php(9584): call_user_func_array(Array, Array)
#14 C:\xampp\htdocs\MyWeb\bootstrap\cache\compiled.php(2982): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#15 [internal function]: Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#16 C:\xampp\htdocs\MyWeb\bootstrap\cache\compiled.php(9584): call_user_func_array(Array, Array)
#17 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#18 C:\xampp\htdocs\MyWeb\bootstrap\cache\compiled.php(9574): call_user_func(Object(Closure), Object(Illuminate\Http\Request))
#19 C:\xampp\htdocs\MyWeb\bootstrap\cache\compiled.php(2254): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#20 C:\xampp\htdocs\MyWeb\bootstrap\cache\compiled.php(2237): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#21 C:\xampp\htdocs\MyWeb\public\index.php(53): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#22 C:\xampp\htdocs\MyWeb\server.php(21): require_once('C:\\xampp\\htdocs...')
#23 {main}  

0 个答案:

没有答案