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
– Joan Marcos
Got it, but now ta so http:/127.0.0.1:8000/Inquire/%201996-10-23
– Joan Marcos
in front of my route with this " %20 " every time you send a request
– Joan Marcos
Take space:
"/ConsultaEmpresa/ "
– Sam
vlw man helped a lot, God too
– Joan Marcos
Take a doubt if I want to pass two parameters in my ajax route as I do ? that will come from my form
– Joan Marcos
You say here?
Route::post('/ConsultaEmpresa/{Data_Inicial}'
– Sam
Also man on both route and ajax, initial date and final date type.
– Joan Marcos
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.– Sam
got it man, I already know how to pass the two in the URL
– Joan Marcos
Already on the route I do not understand Laravel, but it must be something simple too.
– Sam