In the last days I have had a problem that I have not been able to solve, I hope to get your help, please.
The problem is this: I am creating a page which has a module in which inputs are created dynamically but each of those inputs must return a json to me by calling the API
that generates that json
by ajax
and GET
method, I have to validate that each string entered is correct or failing to get a response GET 404 NOT FOUND
I was trying to solve with http request but only validates the first input of all that are created, I have entered the validation in a for cycle in which are all the values of the inputs but always execute the ajax
code at the end of the whole process.
What happens in the code is that the for cycle is executed without executing the code part ajax
, since the code ajax
is executed after executing the for. This confuses me a lot, I hope your help
Code
function procesar(){
var array = [];
var maximo = 1300;
var char2 = CKEDITOR.instances['semblanza'].getData().length;
var maximolineas = 1300;
var char2lineas = CKEDITOR.instances['lineas'].getData().length;
var maximoproy = 1300;
var char2proy = CKEDITOR.instances['proyectos_vigentes'].getData().length;
var cv = document.getElementById('cv').value;
var lineas = document.getElementById('lineas').value;
var semblanza = document.getElementById('proyectos_vigentes').value;
var doiarray = document.getElementsByName('doi2[]');
variable = true;
for(var i = 0; i < doiarray.length; i++){
var data = "http://api.crossref.org/works/"+doiarray[i].value;
if(doiarray[i] != "" && variable == true){
$.ajax({
type: 'GET',
url: data,
success: function(data, textStatus) {
if(variable=true ){
alert(variable+"3");
variable= true;
}
},error: function(xhr, textStatus, errorThrown){
variable = false;
alert("No se debe enviar formulario")
if(xhr.status=='405' || xhr.status=='404'){
alert("Ingresa una cadena valida")
}
console.log("No");
}
});
}else{
if(maximo>=char2 && maximolineas>=char2lineas && maximoproy>=char2proy){
variable = true;
alert(variable+"6");
}else{
variable = false;
alert(variable+"8");
alert("Numero de caracteres sobrepasados")
}
}
}
if (variable == true ){
document.forms['data'].submit()
}
}