Tengo 3 llamadas ajax. Los datos de cada llamada ajax se pasan a john_doe();
Llamada 1
$.ajax({ url: url1, dataType: "JSON", type: "GET", }).success(function(data1){ john_doe(data1); });Llamada 2
$.ajax({ url: url2, dataType: "JSON", type: "GET", }).success(function(data2){ john_doe(data2); });Llamada 3
$.ajax({ url: url3, dataType: "JSON", type: "GET", }).success(function(data3){ john_doe(data3); });Función principal
function john_doe(param){ console.log(param); //Print data from all three ajax call. }¿Cómo separar data1, data2 y data3 en la función john_doe? porque necesito llevar a cabo una operación aritmética.
Corrientemente,
Aporte
data1 = one,two,three data2 = four data3 = fiveProducción
console.log(param) daría salida como
one four fivequiero salida como
console.log(param[0]) console.log(param[1]) console.log(param[2]) param[0] containing one,two,three param[1] containing four param[2] containing fiveNo tengo control sobre los datos. ¿Cómo acceder a data1, data2 y data3 por separado?
Usando promesas, puede acceder a todos los datos en la devolución de llamada de Promise.all() y hacer lo que necesite con todo a la vez. Supone usar jQuery 3+. Puede usar $.when en versiones anteriores
var urls =['data-1.json','data-2.json','data-3.json']; // array of ajax promises var reqPromises = urls.map(function(url){ return $.ajax({ url: url, dataType: "JSON", type: "GET" }); }); Promise.all(reqPromises).then(function(res){ // res is array of all the objects sent to each `$.ajax` from server // in same order that urls are in var param = res.map(function(item){ return item.val }); console.log(param) })La solución rápida y sucia es simplemente pasar un identificador, ¿por qué está sucio porque no es realmente extensible con respecto a agregar, digamos, la cuarta o quinta llamada? Cada vez que hace esto, necesita agregar más identificadores y su instrucción if en el método principal. terminará bastante feo en un punto. Pero dicho esto, a veces "mantenerlo simple" está bien.
Función principal:
function john_doe(identifier, param) { // best to use something more readable then numbers if(identifier == 1) { console.log(param); //Print data from all ajax call 1. } else if(identifier == 2) { console.log(param); //Print data from all ajax call 2. } else if(identifier == 23) { console.log(param); //Print data from all ajax call 3. } else { // handle bad id } }En sus llamadas ajax, pase el identificador correcto, por ejemplo, Llamada 2 :
$.ajax({ url: url2, dataType: "JSON", type: "GET", }).success(function(data2){ // numeric 2 in in the first param is your identifier john_doe(2,data2); });Agregar un parámetro para que sepa desde dónde se está llamando
Llamada 1
$.ajax({ url: url1, dataType: "JSON", type: "GET", }).success(function(data){ john_doe('call1',data); });Llamada 2
$.ajax({ url: url2, dataType: "JSON", type: "GET", }).success(function(data){ john_doe('call2',data); });Llamada 3
$.ajax({ url: url3, dataType: "JSON", type: "GET", }).success(function(data){ john_doe('call3',data); });Función principal
function john_doe(call,data){ console.log('Called from : ' + call); console.log(data); //Print data from all three ajax call. }