Using functions with ajax

0

Good morning friends of this forum.

I have a problem, when executing a js instruction from a function that is executed with the onreadystatechange of the ajax, whose purpose is to update the data of a form, some instructions are not executed. At first I thought it was a syntax error, however, the same instruction is executed from the browser console. This has led me to think that there are some limitations to the instructions that can be used from a function in ajax, in runtime.

If anyone knows anything about it, I appreciate the help. Next I copy the code:

    xact.onreadystatechange = function() {
        if(xact.readyState == 4 && xact.status == 200){
            actDatos =xact.responseText;
            actDatos = JSON.parse(actDatos);
            buscarProvinciaEd(actDatos[0].pais);
            buscarMunicipioEd(actDatos[0].estado);
            candNombre.value = actDatos[0].nombre;
            candDocIde.value = actDatos[0].docidentidad;
            candFnac.value   = actDatos[0].nacimiento;
            candDirecc.value = actDatos[0].direccion;
            candTelef.value  = actDatos[0].telefono;
            candMail.value   = actDatos[0].email;
            candAspSa.value  = actDatos[0].salariopref;
            candObjet.value  = actDatos[0].objetivo;
            //$("#cSexo option[value="+ actDatos[0].sexo +"]").attr("selected",true);
            $("#cprofArea option[value="+ actDatos[0].rama +"]").attr("selected",true);
            $("#cPais option[value="+ actDatos[0].pais +"]").attr("selected",true);
            $("#cEstado option[value="+ actDatos[0].estado +"]").attr("selected",true);
            $("#cMunicipio option[value="+ actDatos[0].municipio +"]").attr("selected",true);
            candSexo.value   = actDatos[0].sexo;
            //candAreaP.value  = actDatos[0].rama;
            //candPais.value   = actDatos[0].pais;
            //candEstado.value = actDatos[0].estado;
            //candMunici.value = actDatos[0].municipio;
        } 
    
asked by Alfred González 29.09.2018 в 13:23
source

1 answer

0

Make sure you're not making a Synchronous call. onreadystatechange is only for asynchronous calls. Instead of xact.readyState == 4 , it would be clearer if you use xact.readyState == XMLHttpRequest.DONE

Is there a reason why you do not use onload ?

You may have already seen this, but here are some examples of how to make calls synchronously and asynchronously: Synchronous and asynchronous requests

    
answered by 29.09.2018 в 18:42