MartinZeltin
10 months ago

Ajax POST MethodNotAllowedHttpException

Posted 10 months ago by MartinZeltin

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

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 );
    });
});

Please sign in or create an account to participate in this conversation.