Problem with Recursive Ajax

3

I have an ajax function which is recursive, which means that it returns me an answer and if it returns a code I'll call it back to itself sending the code it gave me back, and so on until I return it in the code the value of -1, there ends the recursion.

function actualizarProm (curs_para_mate_prof_codi,peri_dist_codi,es_hija,mensaje,firstTime)
{	var xmlhttp;
	/*Agrego la data*/
	var tabla_info = document.getElementById("tabla_info");
	if (firstTime==1)
		while(tabla_info.rows.length > 1)
			tabla_info.deleteRow(-1);
	var fila = tabla_info.insertRow(-1);
	var cellMensaje	= fila.insertCell(0);
	var cellProgreso = fila.insertCell(1);
	cellMensaje.innerHTML = mensaje;
	cellProgreso.innerHTML = '<div id="prog_info_'+peri_dist_codi+'"><img src="../../imagenes/ajax-loader.gif"/></div>';
    var data = new FormData();
    data.append("peri_dist_codi", peri_dist_codi);
	var peri_dist_padr_previous = peri_dist_codi;
	data.append("curs_para_mate_prof_codi", curs_para_mate_prof_codi);
	data.append("es_hija", es_hija);
    data.append("opc", "actualizar_prom");
	if (window.XMLHttpRequest)
  	{	xmlhttp = new XMLHttpRequest ();
  	}
  	else
  	{	xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
  	}
  	xmlhttp.onreadystatechange = function ()
  	{	document.getElementById('prog_info_'+peri_dist_codi).innerHTML="Finalizado";
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
  		{	obj = JSON.parse(xmlhttp.responseText);
			if (obj.error == "no" && obj.peri_dist_codi!=-1 && obj.peri_dist_codi!=null)
			{	actualizarProm(curs_para_mate_prof_codi,obj.peri_dist_codi,es_hija,obj.mensaje,0);
			}
			else
			{	if (obj.error == "no")
				{	if (obj.mensaje==null)
						actualizarProm( curs_para_mate_prof_codi, peri_dist_padr_previous, es_hija, "Reintentando", 0 );
					else
					{	var fila = tabla_info.insertRow(-1);
						var cellMensaje	= fila.insertCell(0);
						var cellProgreso = fila.insertCell(1);
						cellMensaje.innerHTML = obj.mensaje;
						cellProgreso.innerHTML = "Actualización de notas completa";
					}
				}
				else
				{	var fila = tabla_info.insertRow(-1);
					var cellMensaje	= fila.insertCell(0);
					var cellProgreso = fila.insertCell(1);
					cellMensaje.innerHTML = obj.mensaje;
					cellProgreso.innerHTML = "No se ha completado la actualización de notas, intente nuevamente y luego comunique a sistemas.";
				}
			}
  		}
  	}
  	xmlhttp.open("POST", "script_actualizar_prom.php", false);
  	xmlhttp.send(data);
}

The problem here is that even when the php does not return anything, the recursion is still done. That is, how could I make it wait until the server responds and there continue with the next recursion, and so it does not remain cycled. Because what is on the line

if (xmlhttp.readyState==4 && xmlhttp.status==200)

It does not work, since it enters so that the php has not returned anything yet. In the php file I call a stored procedure which runs and takes a while. I hope you have understood me. Thanks for your valuable help.

    
asked by Juan Carlos Rocafuerte 29.04.2016 в 17:21
source

2 answers

1

It's weird what's happening to you. With the if:

if (xmlhttp.readyState==4 && xmlhttp.status==200)

It is getting safe to enter there only in case the connection is terminated ( redyState==4 ) and that there is no error ( status==200 ).

Furthermore, the next thing that has to happen is that a response is received in JSON format (otherwise obj would not exist and could not be controlled and would not enter the recursion.

You could add a console.log(xmlhttp.responseText) before the statement:

actualizarProm(curs_para_mate_prof_codi,obj.peri_dist_codi,es_hija,obj.mensaje,0);

to see what is happening.

    
answered by 29.04.2016 в 18:52
0

I think you got the error where you add the recursive function of ajax.

According to the docs you have to take it in onload()

function reqExito () {
   obj = JSON.parse(this.responseText);
   if (obj.error == "no" && ...) { 
         actualizarProm(curs_para_mate_prof_codi,obj....,0);
          ...
    }
}

var oReq = new XMLHttpRequest();
oReq.onload = reqExito;
oReq.open("POST", "http://www.example.org/example.txt");
oReq.send();

I also recommend using JQuery. You may not be able to, but your code would be much more verbose:

$.post("script_actualizar_prom.php", function (data) { 
    obj = JSON.parse(data);
    if (obj.error == "no" && ...) { 
          actualizarProm(curs_para_mate_prof_codi,obj.peri_dist_codi,es_hija,obj.mensaje,0);
          ...
     }
 })
    
answered by 05.05.2016 в 20:45