Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

chlebta's avatar

Laravel 5 : MethodNotAllowedHttpException

Hello, I'm using Laravel 5 and I got this error :

MethodNotAllowedHttpException in RouteCollection.php line 207:

    in RouteCollection.php line 207
    at RouteCollection->methodNotAllowed(array('POST')) in RouteCollection.php line 194
    at RouteCollection->getRouteForMethods(object(Request), array('POST')) in RouteCollection.php line 142
    at RouteCollection->match(object(Request)) in Router.php line 719
    at Router->findRoute(object(Request)) in Router.php line 642
    at Router->dispatchToRoute(object(Request)) in Router.php line 618
    at Router->dispatch(object(Request)) in Kernel.php line 210
    at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in VerifyCsrfToken.php line 43
    at VerifyCsrfToken->handle(object(Request), object(Closure)) in VerifyCsrfToken.php line 17
    at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 125
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 55
    at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 61
    at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 36
    at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 125
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 40
    at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 125
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 42
    at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 125
    at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
    at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
    at Pipeline->then(object(Closure)) in Kernel.php line 111
    at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 84
    at Kernel->handle(object(Request)) in index.php line 53

This is my route :

Route::post('device/brand', 'DeviceController@addBrand');

and this is my ajax post function :

 $("#addBrand").validate({
            ..
            submitHandler: function(form, event) {
                    run_waitMe();
                    //event.preventDefault();
                    var url = $(form).attr('action');
                   
                    var data = {};
                    data['brandName'] = $("#brandName").val();
                    data['brandLogo'] = $("#brandLogo")[0].files[0];
                    console.log(data);
                $.ajax({
                    type: "POST",
                    url: $(form).attr("action"),
                    data: data,
                    dataType : "json",
                    processData:false,
                    contentType:false,
                    success: function(response){
                        ....
                    }
                })
            }
        });

And this is my Request Info : http://postimg.org/image/lk2sd4woh/

0 likes
16 replies
EliasSoares's avatar

What's the URL that your ajax are using to the request?

Do you have another routes?

Sometimes if you have another route like GET device/{variable}, laravel stops in this first route...

michaeldyrynda's avatar

In your Ajax, change type: "POST" to method: "POST". Came across the same issue recently.

1 like
chlebta's avatar

@EliasSoares I've changed it to brand/add and I don't have any route start with brand but always same issue. @deringer I've changed it to method but didn't solved my issue

EliasSoares's avatar

Strange... Could you use developer tools to show your request headers to us? Also if possible post your entire route files! :)

chlebta's avatar

@EliasSoares check the image I posted that already http://postimg.org/image/lk2sd4woh/ and this my routes file

Route::get('/', 'WelcomeController@index');
Route::post('login', 'WelcomeController@login');
Route::get('logout', function(){
    Auth::logout(); // logout user
    return Redirect::to('/');
});


Route::get('home', ['middleware' => 'auth', 'uses' =>'HomeController@index']);
Route::post('updatePassword', ['middleware' => 'auth', 'uses' =>'HomeController@updatePassword']);


Route::get('analytics', 'HomeController@analytics');



Route::get('device/new', 'DeviceController@addForm');
Route::post('device/add', 'DeviceController@addFunc');


Route::post('brand/add', 'DeviceController@addBrand');

Route::controllers([
    'auth' => 'Auth\AuthController',
    'password' => 'Auth\PasswordController',
]);
EliasSoares's avatar

Is your laravel configured to accept this /neverask/public/ prefix?!

xmgravity's avatar

Is it because csrf token?

You have 2 options:

  1. Exclude URI that you want in Http/Middleware/VerifyCsrfToken.
  2. Put csrf token in tag. ex:
<meta name="csrf_token" content="{ csrf_token() }" />

Include csrf token in ajaxSetup:

    $.ajaxSetup({
        headers: {
            'X-CSRF-Token': $('meta[name="csrf_token"]').attr('content')
        }
    });
meeshka's avatar

I had a similar problem and could only make it to work by excluding the URI in Http/Middleware/VerifyCsrfToken :(

puzbie's avatar

I'm getting this too. The code works fine on my local machine, and on the sites on my server, with one exception. I can't fathom how identical code could cause this on one site but not another.

1 like
Subhash_Chandra's avatar

I am also getting the same error. Its workling on local server but not on the remote server.

byronsargeant's avatar

Having the same issue works fine on local server but not on remote server. Did anyone manage to solve this issue? I am not using ajax either I have commented that out and set a form action still not working.

Getting this error: MethodNotAllowedHttpException

POST not allowed!!

endo64's avatar

I've faced the same issue and after an hour I realized that my request fails with MethodNotAllowed error only on remote, because the remote server requires HTTPS but my request was HTTP. When change my URL to HTTPS it worked.

1 like
byronsargeant's avatar

@endo64 Thank you for your response if this was to help me!

I managed to solve this issue by giving my route a name and setting my form action to post to this route name!

I think I may have confused my routes somewhere along the line so I do need to go back over them and check, but naming the route solved the issue for me!

Wittner's avatar

Just had the same issue. In my case:

var request = new XMLHttpRequest();
      form.addEventListener('submit', function(e) {
      e.preventDefault();
      var formdata = new FormData(form);
      request.open('post', 'upload'); // The offending line
      request.addEventListener("load", transferComplete);
      request.send(formdata);
});

looking at the network tab in Chrome dev tools showed that the post request was going to my page name, plus the 'upload' route, it looked like this:

"http://domain.com/get_files/upload"

I realized that this had nothing to do with post/get, it was simply that my path was being interpreted from the current page/script. I changed the offending line to:

request.open('post', '/upload');
// Note forward slash to say 'this route from the root of the site.

This changed the url to:

"http://domain.com/upload"

and everything started working properly. Hope that helps.

zymawy's avatar

You Should Pass The ID of the cartin recored

1 like

Please or to participate in this conversation.