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.

My AJAX

<script type ="text/javascript">
    $(document).ready(function()
    {
        $.ajaxSetup({
            headers:{
                'X-CSRF-TOKEN' : $('meta[name="csrf-token"]').attr('content')
            }
        });

        $('#form').submit(function(e)
        {
            e.preventDefault();
            data = $(this).serialize();
            $.post('/getSearch', data, function(search)
            {
                $('#data').html('');
                $.each(search, function (key,val){
                    $('#data').append('<tr>'+
                    '<td> '+val.name+'</td>'+
                    '<td> '+val.address+'</td>'+
                '</tr>');

                });

            });
        });
    });

</script>

My Controller

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

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

        }
    }

}

And my routes

//---------------------------------search-----------
Route::get('/search','SearchController@index')->name('index');
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');

6voto

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::get('/search','SearchController@index')->name('index');
Route::post('/getSearch', 'SearchController@getSearch')->name('post');

2voto

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');

0voto

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(...)

0voto

Andry Abreu Points 1

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

$.ajax({
       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.

Greetings

HolaDevs.com

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:

X