I am working with several AJAX requests, some to PHP, others to txt ...
The problem comes when working with the data returned from these requests to form something together (like a table). How can I work with all the information at once?
Investigating a bit I found this article that I recommend your reading that basically gives me two solutions:
Callbacks seem like a sloppy and terribly inefficient solution, but the promises are really interesting but even trying out examples I do not know how to use them.
I'll give you a quick example where I'm trying it:
window.onload = function(){
var global="nulisimo";
var global_segundo="nulisimo_2";
//Cargo el primer TXT
cargar_txt();
var global = procesar_txt();
//Cargo el segundo TXT
cargar_txt_segundo();
var global_segundo = procesar_txt_segundo();
setTimeout(function(){
console.log("Global primera :",global);
console.log("Global segunda :",global_segundo);
}, 2000);
Promise.all([procesar_txt(), procesar_txt_segundo()])
.then(resultArray => console.log("Valores globales :",global,"/",global_segundo))
.catch(e => console.log('Error capturado: ${e}'));
}
Loading TXT is the AJAX request itself, and the only answer it contains is this:
function procesar_txt() {
if(peticion_http.readyState == 4)
{
if(peticion_http.status == 200)
{
var cadena = peticion_http.responseText;
console.log("La cadena en el 1 es :",cadena);
return cadena;
}
}
}
I can not get back the values of the functions, neither by CallBacks nor by promise. Any idea or example of how to handle the results of several AJAX requests? PD: Is the correct way to treat answers with return?
Thank you very much.