我在很多线程中搜索过,但是任何线程都无法解决我的问题。
我使用XAMPP(Lubuntu 15.10上的LAMPP)作为服务器。我创建了一个新项目:
composer create-project laravel/laravel angulara
我在app/HTTP/routes.php
中添加了一条新路线:
Route::get('/', function () {
return view('index');
});
Route::any('/test', function($check = 'OK'){
return 'Check: '.$check;
});
我使用curl发送请求并获得响应。当我使用curl -X "GET" http://localhost/test
时,我会收到回复Check: OK
。但是当我使用curl -X "POST" http://localhost/test
时,我得到了回复:
Whoops, looks like something went wrong.
1/1 TokenMismatchException in VerifyCsrfToken.php line 53:
in VerifyCsrfToken.php line 53
at VerifyCsrfToken->handle(object(Request), object(Closure))
at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 62
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 59
at EncryptCookies->handle(object(Request), object(Closure))
at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 44
at CheckForMaintenanceMode->handle(object(Request), object(Closure))
at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 103
at Pipeline->then(object(Closure)) in Kernel.php line 122
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 87
at Kernel->handle(object(Request)) in index.php line 54
我是Laravel的新手,所以我不知道,在哪里寻找错误,我在http://www.tutorials.kode-blog.com/laravel-5-angularjs-tutorial学习,所以我需要通过AngularJS的$ HTTP方法发布请求。
我该如何解决这个问题?
答案 0 :(得分:4)
我是Csrf令牌问题。 你不能在没有" _token"的情况下使用post方法。变量或者你修改路由代码。你可以得到令牌但是这个方法:
<?php echo csrf_token(); ?>
或者您可以使用以下形式:
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
干杯!
答案 1 :(得分:2)
查看Laravel文档CSRF Protection。
然后转到Kernel.php
文件(app / Http / Kernel.php)并禁用VerifyCsrfToken
中间件。
**
* The application's global HTTP middleware stack.
*
* @var array
*/
protected $middleware = [
...
//\App\Http\Middleware\VerifyCsrfToken::class,
];