My problem is that i send all the information fine by JS, but my controller doesn't receive data. I need send all the information inside the form and an object array Im using Laravel and Ajax /Jquery
JS / Jquery /Ajax code
$(document).ready(function()
{
$('#BtnRegistrar').click(function(event) {
event.preventDefault();
var dataString = $('#formulario').serialize();
$.ajax({
type: "POST",
data: {data:dataString, list:cursos}, //Here load the form data and my array of objects
contentType: 'application/json; charset=utf-8',
headers: {'X-CSRF-Token': $('meta[name="csrf-token"]').attr('content')},
success: function(data) {
console.log('Done');
}
});
});
});
Laravel.blade.php
<form method="post" action="/coordinacionCarrera/agregarNombramiento" id="formulario">
@csrf
<div class="row">
<div class="row">
<div class="col-md-6">
<label for="docente">Escoja el docente.</label>
<div class="input-group">
<span class="input-group-text" id="basic-addon1">
<i class="fas fa-id-card"></i>
</span>
<select required class="form-select" name="usuario" id="usuario">
<option selected>Seleccione</option>
@foreach ($usuario as $user)
<option value={{$user->id}}>{{$user->persona->Cedula}} | {{$user->persona->Nombre}} {{$user->persona->PrimerApellido}} {{$user->persona->SegundoApellido}}</option>
@endforeach
</select>
</div>
</div>
<div class="col-md-3">
<label for="Desde">Desde.</label>
<div class="input-group mb-3">
<span class="input-group-text">
<i class="fas fa-calendar-day"></i>
</span>
<input type="date" class="form-control" name="Desde" id="vigencia" value="{{old('Desde')}}">
</div>
</div>
<div class="col-md-3">
<label for="Hasta">Hasta.</label>
<div class="input-group mb-3">
<span class="input-group-text">
<i class="fas fa-calendar-day"></i>
</span>
<input type="date" class="form-control" name="Hasta" id="vigencia2" value="{{old('Hasta')}}">
</div>
</div>
</div>
<div class="row mtop16">
<div class="col-md-4">
<label for="Categoria">Categoría.</label>
<div class="input-group mb-3">
<span class="input-group-text">
<i class="fas fa-calendar-day"></i>
</span>
<select disabled class="form-select" name="categoria" id="categoria">
<input type="hidden" name="CategoriaActual" id="CategoriaActual" value="0">
</select>
</div>
</div>
<div class="col-md-4">
<label for="correo">Correo.</label>
<div class="input-group mb-3">
<span class="input-group-text">
<i class="fas fa-envelope"></i>
</span>
<select disabled class="form-select" name="correo" id="correo">
<input type="hidden" name="CorreoActual" id="CorreoActual" value="0">
</select>
</div>
</div>
<div class="col-md-4">
<label for="Telefono">Teléfono.</label>
<div class="input-group mb-3">
<span class="input-group-text">
<i class="fas fa-phone-alt"></i>
</span>
<select disabled class="form-select" name="telefono" id="telefono">
<input type="hidden" name="TelefonoActual" id="TelefonoActual" value="0">
</select>
</div>
</div>
</div>
<div class="row mtop16">
<div class="col-md-4">
<label for="Curso">Escoja el curso.</label>
<div class="input-group mb-3">
<span class="input-group-text">
<i class="fas fa-book"></i>
</span>
<select required class="form-select" name="curso" id="curso">
<option selected value="0">Seleccione</option>
@foreach ($curso as $cursos)
<option value={{$cursos->IDCurso}}>{{$cursos->Codigo}} | {{$cursos->Nombre}}</option>
@endforeach
</select>
</div>
</div>
<div class="col-md-3">
<label for="Tipo">I/C.</label>
<div class="input-group mb-3">
<span class="input-group-text">
<i class="fas fa-book"></i>
</span>
<select required class="form-select" name="Tipo" id="Tipo">
<option selected value="0">Seleccione</option>
@foreach (obtenerTipoDeCurso() as $Tipo=>$id)
<option value="{{$id}}">{{$Tipo}}</option>
@endforeach
</select>
</div>
</div>
<div class="col-md-1">
<label for="Tutoria">Tutoría.</label>
<div class="input-group mb-3">
<input class="ms-3 mt-3" name="Tutoria" id="Tutoria" type="checkbox" value="0">
</div>
</div>
<div class="col-md-3">
<label for="CargaAcademica">Carga Académica.</label>
<div class="input-group mb-3">
<span class="input-group-text">
<i class="fas fa-book"></i>
</span>
<select required class="form-select" name="CargaAcademica" id="CargaAcademica">
<option selected value="0">Seleccione</option>
@foreach (ObtenerCargaAcademica() as $CargaAcademica=>$id)
<option value="{{$id}}">{{$CargaAcademica}}</option>
@endforeach
</select>
</div>
</div>
<div class="col-md-1 mtop16">
<a onclick='AgregarCursoDocente()' class="btn btn-plus">
<i class="fas fa-plus-square fs-2"></i>
</a>
</div>
</div>
<div class="row mtop16" id="CursoDocente">
</div>
</div>
<div class="row ">
<div class="col-md-12">
<button type="submit" class="mtop16 btn btn-success" id="BtnRegistrar">Guardar</button>
</div>
</div>
</form>
Controller function
public function postAñadirNombramiento(Request $request)
{
return $request; //Here im trying to capture the data
}
Console browser when i press submit button
Something knows why i'm not receiving nothing? Or have any other method that i can send the form inputs and an array of objects created in Javascript?
CodePudding user response:
the problem is here
success: function(data) {
console.log('Done');
}
the request is successfully reaching your controller you are just logging "done" every time replace it with
success: function(data) {
console.log(data);
}