4 votes

405 (Method Not Allowed) In ajax search laravel

I'm trying to do a search with AJAX and it says 405 (Method Not Allowed) .

I don't know what it could be, I already tried looking at the routes and I can't find the error.


<script type ="text/javascript">
                'X-CSRF-TOKEN' : $('meta[name="csrf-token"]').attr('content')

            data = $(this).serialize();
            $.post('/getSearch', data, function(search)
                $.each(search, function (key,val){
                    '<td> '+val.name+'</td>'+
                    '<td> '+val.address+'</td>'+




My Controller

    class SearchController extends Controller
    public function index(Request $req)
        $datas= search::all();
        return view ('search', compact ('datas'));

    public function getSearch (Request $req)
            $find= search::where('name', 'LIKE','%' .$req->search. '%' )->get();
            return response()->json($find);



And my routes

Route::get('/getSearch', 'SearchController@getSearch')->name('post');

1 votes

You are declaring the routes with the method get but in ajax you use post If you have multiple verbs, you can use the following method to indicate multiple verbs: Route::match(['get', 'post'], '/ruta', 'SearchController@getSearch')->name('post');


First of all, you should look at what is HTTP error 405 :

The HyperText Transfer Protocol (HTTP) 405 Method Not Allowed response status code indicates that the request method is known by the server but has been disabled and cannot be used.

That is to say,

The HTTP 405 error response code Method not allowed , indicates that the method of the request is known to the server, and that the server has has been disabled or is not configured and therefore cannot be used.

Therefore, what happens is that in your server you have not declared the functionality for the POST method .

That is, if I am not mistaken, you have to do:

Route::post('/getSearch', 'SearchController@getSearch')->name('post');


Xerif Points 5599

You are declaring the route /getSearch with the method get but in the ajax use post to call her.

To allow multiple verbs:

Route::match(['get', 'post'], '/getSearch', 'SearchController@getSearch')->name('post');

If you want to allow only post

Route::post('/getSearch', 'SearchController@getSearch')->name('post');

If you want to allow only get

Route::get('/getSearch', 'SearchController@getSearch')->name('post');


Pedro Prada Points 1447

You are making the request by POST and the route is defined with the method GET . Change the routing method to POST Route::post('...') or make the request via GET $.get(...)


Andry Abreu Points 1

I once had the same error in Laravel 5.6 and I solved it as follows:

       type: 'POST',
       url: "**{{ route('buscarCedula') }}**",
       data: {cedula: cedula},
       dataType: 'html',


make sure that in the URL route, indicate the specific route with the laravel route reserved word. To that route I send via ajax, the value cedula which is sent to a controller. Before it gave me error when I used: url: "searchCedula" .

The same thing happened to me once in the action of a form, I got the same error using:

action="searchCedula" referring to the name of a path in web.php

but I did place it:

action="{{ route('buscarCedula') }}"

worked flawlessly.



HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by: