Reputation: 3439
I am trying to make an ajax POST request to a controller function but I keep getting this error. I followed the advice I found online and added $.ajaxSetup
with X-CSRF-TOKEN
but still no luck.
"exception": "Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException"
web.php
Route::get('my-controller/mypostfunction', 'MyController@mypostfunction');
MyController.php
public function mypostfunction()
{
return "Hello poster!";
}
app.js
$( document ).ready(function()
{
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
// This alerts the CSRF token correctly!
alert( $('meta[name="csrf-token"]').attr('content') );
$.post( "my-controller/mypostfunction", function( data ) {
alert( "Data Loaded: " + data );
});
});
Upvotes: 1
Views: 329
Reputation: 1981
In your web.php change route for get to post like this:
Route::get('my-controller/mypostfunction', 'MyController@mypostfunction');
// into
Route::post('my-controller/mypostfunction', 'MyController@mypostfunction');
I hope this is the solution.
Upvotes: 1
Reputation: 15175
in your web.php file u set get method so just change get
to post
method type
Route::post('my-controller/mypostfunction', 'MyController@mypostfunction');
Upvotes: 2