Sándor Veres Sándor Veres - 1 year ago 45
PHP Question

Laravel 5.1 POST with ajax, get error. Do it with GET and it works

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"=>'[email protected]'
]);


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.

Answer Source

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 :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download