I have a $ ('form'). on ('submit', function ()); and inside it I would like to make a call from a function that runs an ajax and returns an array of data. however when saving the return of the function in a variable it becomes "undifined" can not do what I want?
Follow the example code:
Fom submit:
$('#form_independentes').on('submit', function(event) {
let curso_id = 1;
let ficheiros = getDocuments_(curso_id);
// variavél ficheiro fica como undefined
});
getDocuments_ function:
function getDocuments_(id_curso){
$.ajax({
url: '/candidaturas/documents/'+id_curso,
type: 'GET',
dataType: 'json',
success: function( _response ){
//este console log mostra um array de objetos
console.log(_response.documents)
return _response.documents;
},
error: function( _response ){
Materialize.toast('Opps ocorreu um erro. por favor atualize a página e volte a tentar', 4000, 'red');
return false;
}
});
}
Also follows a console print showing the undefined and array of objects