1
votes

So it's my first laravel project and i can't find out what's the problem.

I searched for hours, but nothing helped.

{!! csrf_field() !!} is in my form, the _token data is sended

Route:

Route::post('/posts/create', [
    'middleware' => 'auth',
    "as" => 'post-create',
    "uses"=>'PostsController@create'
]);

Ajax:

 $.ajax({
     url:'{{route('post-create')}}',
     type:'POST',
     data:form.serialize()
 });

Return for ajax:

Remote Address:[::1]:80
Request URL:http://localhost/domain/public/posts/create
Request Method:POST
Status Code:500 Internal Server Error

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 54
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 42
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 118
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 86
at Kernel->handle(object(Request)) in index.php line 54

And the laravel standard user register doesn't work too. When the login does (both works with post).

What did i miss?

EDIT: interesting that if i do it with a submit button it reaches the controller. So i found out the problem is coming from the controller:

$title=Input::post('title');

There is the 'use Illuminate\Support\Facades\Input;' line

EDIT2: $title=Input::post('title'); was the problem, i guess. I should use $request->input('title');

But i don't know how to use it properly, i get

Target [App\Http\Requests\Request] is not instantiable.

error if i add 'Request $request' param to the controller function.

3
The error message would help...lukasgeiter
Is the JavaScript code you posted inlined in a Blade view file, or is it part of a separate JS file?Bogdan
Can you add success and error method for Ajax and log what laravel returned for request ?dyachenko
I'm not at home now, i will add the response later. The ajax is in the blade. And if it helps i got 500error if i remember well.Sándor Veres

3 Answers

0
votes

The problem here is that Laravel cannot see your csrf token. Firstly set it in the meta description by using

<meta name="csrf-token" content="{{ csrf_token() }}">

Then in your JQuery use

$.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
});

This should resolve your issue

http://laravel.com/docs/5.1/routing#csrf-protection

0
votes

In my controller i used '$title=Input::post('title');' instead of '$request->input('title');'

I Had to make Request class, i made App\Http\Requests\PostCreateRequest.

php artisan make:request PostCreateRequest

Then in the controller

public function create(PostCreateRequest $request){
        print $request->input('title');
}

It works :)

0
votes

You can use this:
https://laravel.io/forum/11-14-2014-disabling-the-csrf-middleware-in-laravel-5

In this, you can disable csrf_token for given routes with use of new class by replacing VerifyCsrfToken class.