MethodNotAllowedHttpException ($ others) Laravel Form submission via Ajax

0

I have a simple form and I am sending it to the controller via Ajax, but my problem is that after clicking on the submit button it does not call the Ajax request in my Jquery and it already leads directly to this error. p>

Ihaveanothercontrollerandcopiedtheentireprocessbychangingonlytheinputsandfields

JavaScriptcodethatthesystemshouldpassaftersubmits

jQuery("#formRecebimento").submit(function(){
            var dados = jQuery( this ).serialize();            
            jQuery.ajax({
                type: 'post',
                url: 'recebimentos/cadastrar',
                data: dados,
                beforeSend: function(xhr){xhr.setRequestHeader('X-CSRF-TOKEN', $("#token").attr('content'));},                
                success: function(data) {
                    if($.isEmptyObject(data.error)){
                        alert(data.id);                        
                        $('#modalAddCliente').modal('hide');

                    }else{
                        printErrorMsg(data.error);
                    }
                }


            });

            return false;
    });

But he does not even get to that part of the code!

PS: IN ANOTHER CONTROLLER I MOUNTED THE SAME WAY AND WORKED CORRECTLY

ROUTES

$this->group(['prefix' => 'recebimentos'], function(){
    $this->post('cadastrar', 'RecebimentoController@store');
    $this->post('atualizar', 'RecebimentoController@update');
    $this->post('detelar', 'RecebimentoController@destroy');

    $this->get('/', 'RecebimentoController@index');
});

PS CONTROLLER: THE INDEX IS WORKING NORMALLY AND I LEAVE THE STORE COMMENTED TO FIND WHERE THE ERROR IS AND IT DOES NOT ARRIVE AT MY CONTROLLER

public function store(Request $request){      /*
         $validator = Validator::make($request->all(), [
            'data_receb' => 'required|date|after:start_date',
            'valor' => 'required',
            'cliente_id' => 'required',
            'plano_contas' => 'required|max:100',                  
        ]);
        if ($validator->passes()) {

            $recebimento = Recebimento::create($request->all());
            return response()->json($recebimento);
        }
        return response()->json(['error'=>$validator->errors()->all()]);      */  

        return "asefa";
    }

PART OF THE FORM DEFINITION

<form id="formRecebimento" method="post">

                            <input type="hidden" name="_token" id="token" value="{{ csrf_token() }}">
                            <div class="row">
                                    <div class="col-md-4">
                                        <div class="form-group">
                                            <label for="data_receb">Data do Recebimento*</label>
                                            <input type="date" class="form-control data_receb" id="data_receb" name="data_receb" >
                                        </div>
                                    </div>

GLOBAL SCRIPT PS: I HAVE A JS FILE FOR EACH CONTROLLER

$.ajaxSetup({
                headers: {
                    'X-XSRF-Token': $('meta[name="_token"]').attr('content')
                }
            });
    
asked by anonymous 07.02.2018 / 17:33

1 answer

0

You are not handling the submit event in jquery properly.

What is happening is:

When you click on submit form, it triggers the submit event, Jquery takes that event by default it continues (with the action attribute of the form) and not with the ajax you created.

How to resolve:

The right thing to do is to block this "default" and treat within $('#form').submit();

Just do this:

jQuery("#formRecebimento").submit(function(e){
    e.preventDefault();
    //Resto do Código
});

Why use PreventDefault?

Why it cancels the event if it is cancelable, without stopping the propagation of it.

In this case it continues in the Submit event of <form> But who controls this is its JS code

Some References:

link

link

    
07.02.2018 / 18:07