Jquery-Ajax Orange Query 5.5

Asked

Viewed 941 times

1

I can’t get the ajax consult on Laravel5.5.

Query 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>

Query JS( Ajax for 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);
    }    

Route of my consultation

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

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

XHR finished loading: POST "http://127.0.0.1:8000/Query company/%20undefined"

  • OK I’ll try man

  • Got it, but now ta so http:/127.0.0.1:8000/Inquire/%201996-10-23

  • in front of my route with this " %20 " every time you send a request

  • Take space: "/ConsultaEmpresa/ "

  • vlw man helped a lot, God too

  • Take a doubt if I want to pass two parameters in my ajax route as I do ? that will come from my form

  • You say here? Route::post('/ConsultaEmpresa/{Data_Inicial}'

  • Also man on both route and ajax, initial date and final date type.

  • In Ajax you can do something like this: url: "/ConsultaEmpresa/ " + $('#Data_Inicial').val()+$('#Data_Final').val(), but the two dates will be together... you have to see how you want to send the two things, how they will be separated in the URL.

  • got it man, I already know how to pass the two in the URL

  • Already on the route I do not understand Laravel, but it must be something simple too.

Show 6 more comments

1 answer

1


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

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

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 white space after the last bar on "/ConsultaEmpresa/ ". Seems to be incorrect too.

  • vlw man <3 tamo together

Browser other questions tagged

You are not signed in. Login or sign up in order to post.