Error 405 (Method Not Allowed) Laravel 5

Error 405 (Method Not Allowed) Laravel 5

  1. Blog
  2. 15 November 2026
  3. 5 min read

Stefan Bogdanescu

Founder & Senior Architect

Why Your jQuery POST to a Laravel DELETE Route Fails with 405

You are encountering a 405 Method Not Allowed error because you are attempting to send an HTTP POST request to an endpoint that the application is configured to accept only as DELETE. This isn't a bug in your JavaScript or your route definition; it is a fundamental mismatch between the HTTP verb used for transport and the security model defined by your framework.

In your example, the controller method delete and the HTML form are explicitly marked for the DELETE method:

{!! Form::open(['route' => ['companiesDelete', ':USER_ID'], 'method' => 'DELETE', 'id' => 'formDelete']) !!}

However, your jQuery logic constructs a standard $.post() call. While _method=DELETE is included in the serialized data payload, this mechanism relies on the server accepting POST requests and then inspecting that specific field to simulate a DELETE action. If the route itself is defined strictly as a POST handler but lacks the necessary configuration to recognize the _method token, or if the underlying routing logic rejects the verb before reaching your controller, you get the 405 response.

The Misconception of HTTP Verbs

The core issue lies in how HTTP verbs map to server behavior. Laravel follows RFC standards where DELETE requests must be sent using the DELETE verb over the wire. When you use $.post(), the browser sends an Action: POST header. Even though your data payload contains _method=DELETE, a strict server configuration will reject the request immediately because the HTTP method does not match the route's expectation.

If you change to $.get() and it works, it suggests that your route might be defined as accepting GET requests (perhaps for caching or CSRF exemption purposes) or that your specific deployment is ignoring the verb entirely. However, relying on GET for state-changing operations like deletion is a security anti-pattern. GET requests should be idempotent and safe; they should not trigger side effects like removing data from the database.

Correcting the Transport Layer

To resolve this while keeping the logic in jQuery, you must align the transport method with the HTTP verb expected by the route. The most direct fix is to use $.ajax() or the native $.post but ensure the request options explicitly specify type: 'DELETE'. Modern browsers and libraries handle this gracefully, provided the server accepts it.

Here is how you should adjust your jQuery code to send a true DELETE request:

$('.delete').click(function (e) {
    e.preventDefault();
    var row = $(this).parents('tr');
    var id = row.data('id');
    
    // Ensure the URL matches your route exactly
    var url = 'http://localhost/laravel5.1/public/empresas/eliminar/' + id;

    $.ajax({
        url: url,
        type: 'DELETE', // Explicitly set the HTTP verb to DELETE
        data: {
            _token: $('meta[name="_token"]').attr('content') // Retrieve token securely
            // Note: Do not serialize the form here if you don't need other POST fields
        },
        success: function (result) {
            alert(result);
        }
    });
});

By setting type: 'DELETE', you are no longer relying on the _method field hack. You are telling the browser to send a DELETE verb, which matches your route definition Route::post(...) only if Laravel's internal routing logic is configured to treat this path as a DELETE endpoint. Wait—if your route is defined as Route::post, it will still reject a DELETE verb unless you change the route definition itself.

Aligning the Route Definition

This brings us to the critical decision: should your route be POST or DELETE? If your controller method is named delete and performs destructive actions, the route should ideally be defined with the DELETE verb.

If you must keep the route as POST (perhaps for legacy reasons or specific middleware requirements), you cannot simply send a DELETE request from the client without triggering a 405. Instead, you must rely on the _method field and ensure the route accepts POST requests while your controller logic checks for that field.

However, looking at your source material, you have an EDIT showing:
Route::post('empresas/eliminar/{id}', ...)

If this is your final configuration, sending a DELETE verb from jQuery will fail. You have two paths forward:

  1. Change the Route Verb: Update your route definition to accept DELETE requests explicitly. This is the cleanest approach for RESTful APIs.

    // Preferable for RESTful design
    Route::delete('empresas/eliminar/{id}', ['as' => 'companiesDelete', 'uses' => 'CompaniesController@delete']);
    

    Then, use type: 'DELETE' in your AJAX call.

  2. Keep the POST Verb: If you must keep the route as POST, ensure your jQuery sends a POST request but includes _method=DELETE in the data. Crucially, you must also configure Laravel to ignore CSRF protection for this specific route if it is expected to be state-changing via POST simulation. However, standard Laravel 5 security will block POST requests without a valid token unless the route is exempted.

The most robust solution is to change the route to DELETE. This ensures that $.post() with _method=DELETE is no longer required, reducing payload complexity and adhering strictly to HTTP standards. Your controller logic remains unchanged, but your client-side code must match the server's expectation of the HTTP verb.

Final Recommendation

Do not use GET requests for deletion operations. While it might work temporarily, it violates REST principles and exposes your application to accidental data loss if a user bookmarks the delete link. The 405 error is Laravel's way of enforcing that you have not correctly paired the client request with the server route.

Align your Route::delete() definition with your controller method name, or ensure your client sends a POST request with _method=DELETE only if the route is explicitly defined to handle POST traffic. In either case, verify that CSRF tokens are handled correctly for the chosen HTTP verb to prevent security vulnerabilities.

Our network