error 405 POST ajax laravel MethodNotAllowedHttpException

3

I have a form in which I try to add a category by means of ajax and whenever I try to create a category I get POST 405 error, and it never reaches Controller category and obviously does not enter anything in the database

I use windows 7 and Xampp and all this is on localhost

My data

Route::resource('categorias','categoriaController');

CREACATEGORIAS

{!! Form::open(['route' => 'admin.categorias.store','method' =>'POST','class'=>'form-horizontal']) !!}

                                    {{ csrf_field() }}

                                   <div  class="form-group">
                                          {!! Form::label('categoria', 'Categoria', ['class' => 'col-md-4 control-label']) !!}
                                        <div class="col-md-6">

                                         {!! Form::text('categoria', '', ['Crea una categoria','class'=>'form-control']) !!}


                                        </div>
                                    </div>

                                      <div class="form-group">
                                        <div class="col-md-6 col-md-offset-4">
                                            <button type="submit" class="btn btn-primary" id="bsubmit">
                                                <i class="fa fa-btn fa-user"></i> Crea tu categoria
                                            </button>
                                        </div>
                                    </div>
{!! Form::close() !!}

AJAX FUNCTION

$(document).ready(function() {


    $('#bsubmit').click(function(e){
        e.preventDefault();
        var valorCategoria = $('input[name="categoria"]').val();
        //var rutaCompleta = window.location.pathname+'/create';
    var rutaCompleta = window.location.pathname;

        $.ajax({
            url: rutaCompleta,
            method: 'POST',
            dataType: 'json',
            data: {'categoria': valorCategoria},
            headers: { 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content') },
        })
        .done(function() {
            console.log("success");
                    var estilo = 'style="background : green; height : 30px; text-align:center; color:white; margin-top:20px "';
                    var msgnotificacion =   '<div class="alert alert-success">';
                        msgnotificacion+=   '<a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>';
                        msgnotificacion+=   '<strong>REGISTRO '+valorCategoria+' CREADO CORRECTAMENTE</strong>';
                        msgnotificacion+=   '</div>';

                     $('#labelnombre').after(msgnotificacion);
                     $('#bsubmit').after('<p '+estilo+'>Categoria..:: '+valorCategoria+' ::.. </p>');

        })
        .fail(function(erroz) {
            console.log("error_"+erroz.status);
        })
        .always(function() {
            console.log("complete");
        });




    })
});

The goals I have in sight

<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="_token" content="{!!csrf_token()!!}">

Category Control (store method)

  public function store(Request $request)
    {

        dd($request->all());
         if ($request->ajax()) :
            \App\modelos\categoria::create($request->all());

        endif;

    }
    
asked by KurodoAkabane 21.11.2016 в 18:11
source

1 answer

3

Error 405 is "Method or verb not allowed".

If you are using Route:resource(algo) you must follow the conventions of Laravel, (unless you create your own generation of routes):

According to Laravel's documentation, the path algo/create is what the creation form should show, so it must be a GET.

The path that the POST verb uses to save the information is algo/store .

In short, you must adapt your driver better to match the verbs and routes of Route::resource or add exceptions in the method, or not use it and define each manual route.

Here is the link where it shows the table of equivalences for Route::resource : link

    
answered by 21.11.2016 / 18:23
source