Ver Mensaje Individual
  #3 (permalink)  
Antiguo 29/03/2012, 08:56
Avatar de maycolalvarez
maycolalvarez
Colaborador
 
Fecha de Ingreso: julio-2008
Ubicación: Caracas
Mensajes: 12.120
Antigüedad: 15 años, 9 meses
Puntos: 1532
Respuesta: Funcion devuelve valor demasiado pronto

me imagino que lo que haces es esto:

alert(getText('type','text'));

y como tu mismo lo has dicho ajax es asincrono por lo que realmente la funcion getText devolverá undefined.

tienes 2 opciones:

1- hacer el AJAX sincrono (poco recomendable) y que el navegador se congele esperando la respuesta.

2- pasar el calllback directamente para recibir la respuesta del server y así emitir el alert:

Código Javascript:
Ver original
  1. function getText(type,text, mycallback)
  2. {
  3. $.ajax({
  4.             type: "POST",
  5.                url: "scripts/php/getxml.php",
  6.                data: "fichero=strings.xml&type="+type+"&text="+text,
  7.                success: mycallback,
  8.             error: function()
  9.             {
  10.             alert("Error de conexion, por favor vuelva a intentarlo.");
  11.             return false;
  12.             }
  13.   });
  14.  }
  15.  
  16. getText('type','text', function (text) {
  17.     alert(text);
  18. })
__________________
¡Por favor!: usa el highlight para mostrar código
El que busca, encuentra...