0

to resume my problem, i'm using many XMLHttpRequest() rockets, with a view to get the value (miniTable) returned by the TableRow() function. The problem is, with the alert() on the end of the TableRow() function, i'm have exactly the value that i want, but on TableContent2 variable i'm having an "Undefined" value. I don't know why!! here all the JS file that i'am using (don't care about variables and code calculating the variables). I really need your help, because i'm blocked since 3 days on that. Thank you again and good afternoon freinds.

(function() {
 xmlhttp = new XMLHttpRequest(); 
 xmlhttp.onreadystatechange = function() { 
 if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
    myFunction(xmlhttp); 
 }
};
xmlhttp.open("GET", "File1.xml", true); 
xmlhttp.send(); 
})();
function  ContentFunction(func) {
TableContent2 = TableRow(); 
alert(TableContent2);  
 }
function TableRow() {
xmlhttp3 = new XMLHttpRequest(); 
xmlhttp3.onreadystatechange = function() { 
    if (xmlhttp3.readyState == 4 && xmlhttp3.status == 200) { 
    texttest = myFunction2(xmlhttp3); 

    alert(miniTable); 
    return miniTable; 
    }
};
xmlhttp3.open("GET", "File2.xml", true); 
xmlhttp3.send(); 
}
function myFunction2(xml) {
var xmlDoc2 = xml.responseXML; 
var ObjectText; 
var x = xmlDoc2.getElementsByTagName("Clip"); 
/*Calcule de ObjectText*/
  alert(ObjectText); 
return ObjectText;       
}
function myFunction(xml) {
xmlhttp2 = new XMLHttpRequest(); 
var xmlDoc = xml.responseXML; 
var x = xmlDoc.getElementsByTagName("Film"); 
xmlhttp2.onreadystatechange = function() { 
    if (xmlhttp2.readyState == 4 && xmlhttp2.status == 200) { 
        myFunction2(xmlhttp2);
    }
};
xmlhttp2.open("GET", "File2.xml", true); 
xmlhttp2.send();  
}   
HappyDAD
  • 21
  • 1
  • An XMLHttpRequest function can't `retun` because is asynchronous. – yuriy636 Sep 10 '16 at 12:01
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – yuriy636 Sep 10 '16 at 12:02
  • I have tried all the callback functions and tried to turn the true to false, but always thee indefined value to you propose a modification on the code to get around the asynchronisation call ? i use a function how call an XMLHttpRequest() who call an other function that call XMLHttpRequest() – HappyDAD Sep 10 '16 at 12:15

1 Answers1

0

TableRow returns nothing. The return statement at xmlhttp3.onreadystatechange isn't in the earlier scope. Besides that, your xmlhttp3 is set to be asynchronous, then you can't directly return any information of the AJAX. Synchronous requests, which are deprecated (that's why you shouldn't use them), can be directly read, since they act like a infinite loop that breaks when the request is done (for(;xhr.readyState!==4;);, doing this manually will pause the request and the script execution forever, this is why synchronous requests have been made before.).

Synchronous requests aren't a good idea, they break interaction with entire of the page, since they pause the page/script execution. For instance, if you've a animation, it'll be paused, including event listeners.

Also, it looks like miniTable haven't been declared in any part of your code.

Consider using callback functions, they'll be stored in the TableRow scope and can be called later, with extra arguments.

This is a base:

function  ContentFunction(func) {
    TableRow(function(TableContent2) {
        alert(TableContent2);
    });
}

function TableRow(doneFnc) {
    var xmlhttp3 = new XMLHttpRequest;
    xmlhttp3.onreadystatechange = function() {
        if (xmlhttp3.readyState === 4 && xmlhttp3.status === 200) { 
            var texttest = myFunction2(xmlhttp3); 

            /* success callback */
            doneFnc(texttest);
        }
    };
    xmlhttp3.open("GET", "File2.xml", true); 
    xmlhttp3.send(); 
}
  • I appreciate your help and, firstly i want to think you. Concerning your proposition, it enters into TableRow(doneFnc), but it's not showing the alert (no alert at all) inside the call "alert(TableContent2)", and what do you mean with /*success callback*/, is this a part to finish? – HappyDAD Sep 10 '16 at 19:21
  • @HappyDAD Yes, when the request is succesfully done with the 200 status. If it's not alerting anything, I believe your request is never succesfully done. –  Sep 10 '16 at 19:58
  • 1
    Thank you very much i think it's working the solution you proposed, i am sorry for my previous comment, i will test my code well and give you (and all interested persons) the final result. Thank you very much if it's really working you have saved my life. best regards. (From Paris with love) – HappyDAD Sep 10 '16 at 20:13