405 (Method Not Allowed) In Search ajax laravel

2

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

I do not know what I can be, I tried looking at the routes and I can not 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');
    
asked by Bella 24.01.2018 в 09:59
source

4 answers

5

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

  

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

That is,

  

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

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

That is, if I'm not wrong, you have to do:

Route::get('/search','SearchController@index')->name('index');
Route::post('/getSearch', 'SearchController@getSearch')->name('post');
    
answered by 24.01.2018 / 11:01
source
2

You are stating the path /getSearch with the method get but in ajax you use post to call it.

To allow multiple verbs:

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

Yes, you want to allow only post

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

Yes, you want to allow only get

Route::get('/getSearch', 'SearchController@getSearch')->name('post');
    
answered by 24.01.2018 в 12:45
0

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

    
answered by 24.01.2018 в 10:05
0

Once I had the same error in Laravel 5.6 and I solved it in the following way:

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

 }); 

Notice that in the URL path, specify the specific path with the reserved word of laravel route. To that route I send via ajax, the value card which is sent to a controller. I used to get an error when I used to: url: "searchCellula" .

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

action="searchCount" , referring to the name of a route in the web.php

but if I put it:

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

It worked without problems.

Greetings

    
answered by 29.11.2018 в 21:06