Query JQuery-Ajax Laravel 5.5

1
  

I can not do the ajax query in Laravel5.5.

Inquiry Form:

<form action="ConsultaEmpresa" method="get" name="FormConsultaEmpresa" id="FormConsultaEmpresa">
          {{ csrf_field() }}

   <input type="date" style="width: 30%;" class="form-control" name="Data_Inicial" id="Data_Inicial">

   <button class="btn btn-success" style="margin-left: 10px;" id="Consulta_Empresa" type="submit">Pesquisar</button>

</form>

JS query (Ajax for the query):

jQuery(document).ready(function($)  {
    $("#FormConsultaEmpresa").submit(function (e) {
        e.preventDefault();
        $.ajax({
            type: "get",
            url: "/ConsultaEmpresa/ " + $('#Data_Inicial').attr("value"),
            dataType: 'json',
            headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
            data : $(this).serialize(),
            success: function (Consulta) {
                    Consulta.responseJSON;
                    console.log(Consulta);
            },complete() {
                    console.log("complete");
            }
        });
    });
});

Controller that returns the Ajax query

public function ConsultaAjax(Request $Data_Inicial)
    {
        /*
        $date = explode("/", $Data_Inicial);

        $dateBanco = $date[2]."/" . $date[1] . "/" . $date[0];*/

        $Consulta = DB::table('empresas')->where([ ['Empresa_Dtinicioacesso', '=', $dateBanco] ])->get();        

        return Response::json($Consulta);
    }    

My query path

Route::get('/ConsultaEmpresa/{Data_Inicial}', ['uses' => 'EmpresaController@ConsultaAjax']);

This message appears in the log (Insert element) when the request ends.

  

XHR finished loading: POST   " link "

    
asked by anonymous 24.04.2018 / 17:32

1 answer

1

You're trying to get the value wrong: $('#Data_Inicial').attr("value") .

Use $('#Data_Inicial').val() to get the value of the field, since it does not have the attribute value explicit.

It would look like this:

jQuery(document).ready(function($)  {
    $("#FormConsultaEmpresa").submit(function (e) {
        e.preventDefault();
        $.ajax({
            type: "post",
            url: "/ConsultaEmpresa/" + $('#Data_Inicial').val(),
            dataType: 'json',
            headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
            data : $(this).serialize(),
            success: function (Consulta) {
                    Consulta.responseJSON;
                    console.log(Consulta);
            },complete() {
                    console.log("complete");
            }
        });
    });
});
  

Also remove this blank after the last bar in    "/ConsultaEmpresa/ " . It seems to be incorrect too.

    
24.04.2018 / 17:44