I've been watching for a while the questions here in Stack Overflow about returning arrays on JavaScript, but not a single answer works in my code. It always returns 'undefined'.
This is the important part of the code:
function VisualizadorIzquierdo () {
// some stuff here
this.dibujar = function () {
var datos = obtenerDatos(0);
alert(datos); //<--- This always returns 'undefined'
// other stuff here
}
// more stuff here
}
function obtenerDatos(id) {
var xmlhttp = ConstructorXMLHttpRequest();
if(!xmlhttp) alert('Error: No se pudo crear el objeto XMLHttpRequest');
else {
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState == 4)
{
alert(xmlhttp.responseText); //<--- This returns '[1,2,3,4]'
return xmlhttp.responseText;
}
}
xmlhttp.open('GET', rutaLector + '?archivo=' + archivos[id], false);
xmlhttp.send(null);
}
}
I'v tried defining the variable datos as a new Array() and new Object(). Also I'm trying returning an Object (like {"arreglo" : [1,2,3,4]}), defining a new Array() and new Object() within obtenerDatos(id) function, changing between syncronic and asyncronic, etc ...
But every time it returns 'undefined' :c
So, How can I definitely return an Array from a function?
Thanks in advanced! :)