Error 405 (Method Not Allowed) Laravel 5

前端 未结 6 789
遇见更好的自我
遇见更好的自我 2020-12-05 22:53

Im trying to do a POST request with jQuery but im getting a error 405 (Method Not Allowed), Im working with Laravel 5

THis is my code:

jQuery



        
相关标签:
6条回答
  • 2020-12-05 23:18

    The methodNotAllowed exception indicates that a route doesn't exist for the HTTP method you are requesting.

    Your form is set up to make a DELETE request, so your route needs to use Route::delete() to receive this.

    Route::delete('empresas/eliminar/{id}', [
            'as' => 'companiesDelete',
            'uses' => 'CompaniesController@delete'
    ]);
    
    0 讨论(0)
  • 2020-12-05 23:19

    In my case the route in my router was:

    Route::post('/new-order', 'Api\OrderController@initiateOrder')->name('newOrder');

    and from the client app I was posting the request to:

    https://my-domain/api/new-order/

    So, because of the trailing slash I got a 405. Hope it helps someone

    0 讨论(0)
  • 2020-12-05 23:19

    This might help someone so I'll put my inputs here as well.

    I've encountered the same (or similar) problem. Apparently, the problem was the POST request was blocked by Modsec by the following rules: 350147, 340147, 340148, 350148

    After blocking the request, I was redirected to the same endpoint but as a GET request of course and thus the 405.

    I whitelisted those rules and voila, the 405 error was gone.

    Hope this helps someone.

    0 讨论(0)
  • 2020-12-05 23:19

    When use method delete in form then must have to set route delete

    Route::delete("empresas/eliminar/{id}", "CompaniesController@delete");
    
    0 讨论(0)
  • 2020-12-05 23:22

    Your routes.php file needs to be setup correctly.

    What I am assuming your current setup is like:

    Route::post('/empresas/eliminar/{id}','CompanyController@companiesDelete');
    

    or something. Define a route for the delete method instead.

    Route::delete('/empresas/eliminar/{id}','CompanyController@companiesDelete');
    

    Now if you are using a Route resource, the default route name to be used for the 'DELETE' method is .destroy. Define your delete logic in that function instead.

    0 讨论(0)
  • 2020-12-05 23:29

    If you're using the resource routes, then in the HTML body of the form, you can use method_field helper like this:

    <form>
      {{ csrf_field() }}
      {{ method_field('PUT') }}
      <!-- ... -->
    </form>
    

    It will create hidden form input with method type, that is correctly interpereted by Laravel 5.5+.

    Since Laravel 5.6 you can use following Blade directives in the templates:

    <form>
      @method('put')
      @csrf
      <!-- ... -->
    </form>
    

    Hope this might help someone in the future.

    0 讨论(0)
提交回复
热议问题