Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error 405 (Method Not Allowed) Laravel 5

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

<script type="text/javascript">     $(document).ready(function () {         $('.delete').click(function (e){             e.preventDefault();             var row = $(this).parents('tr');             var id = row.data('id');             var form = $('#formDelete');             var url = form.attr('action').replace(':USER_ID', id);             var data = form.serialize();             $.post(url, data, function (result){                 alert(result);             });         });     });     </script> 

HTML

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

Controller

public function delete($id, \Request $request){         return $id;     } 

The Jquery error is http://localhost/laravel5.1/public/empresas/eliminar/5 405 (Method Not Allowed).

The url value is

http://localhost/laravel5.1/public/empresas/eliminar/5 

and the data value is

_method=DELETE&_token=pCETpf1jDT1rY615o62W0UK7hs3UnTNm1t0vmIRZ. 

If i change to $.get request it works fine, but i want to do a post request.

Anyone could help me?

Thanks.

EDIT!!

Route

Route::post('empresas/eliminar/{id}', ['as' => 'companiesDelete', 'uses' => 'CompaniesController@delete']); 
like image 567
German Ortiz Avatar asked Jul 25 '15 04:07

German Ortiz


People also ask

What does 405 Forbidden mean?

A 405 Method Not Allowed Error is an HTTP response status code that indicates a web browser has requested access to one of your web pages and your web server received and recognized its HTTP method.

How do I fix 405 Method not allowed in flask?

To fix POST Error 405 Method Not Allowed with Flask Python, we should make sure the action attribute of the form is set to the URL of the view that accepts POST requests. to create the template view. to add a form that has the action attribute set to the URL for the template view that we get with url_for('template') .


2 Answers

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' ]); 
like image 142
Jeemusu Avatar answered Sep 29 '22 05:09

Jeemusu


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.

like image 36
davsp Avatar answered Sep 29 '22 05:09

davsp