I am trying to create another JSON file, through this API , but I can not , the result is coming out just as [ ]
I need to access this api ( https://api.cartolafc.globo.com/time/+time ) for each value of ARRAY TIMES , receive the results individually and take only the KEYS I need to create a new ARRAY / JSON
$(document).ready(function() {
var result = [];
var times = new Array("sport-clube-balao", "verdao-f-c-s-p", "kibeb-s-fc", "maria-fumaca-futebol-clube", "skolusa-fc", "fc-chikungunya", "furia-verde-1988-f-c", "deixaa-queto-f-c", "peixeraum", "espinoza-fc", "s-e-palestra-parente", "ca7fc", "lebucchi-fc", "impactos-fc", "tricolor-futebol-amigos", "augustinho-s-fs", "os-cara-de-egua", "porcos-locos-fc", "toniolotricolor-fc", "massa-clube", "made-china", "pipoca-jacare", "deprimeira-f-c", "rocca-uryntians", "hu100-f-c", "gabrukas-tln", "zaca-clube", "poderoso-rorinthians", "kbca-futebol-clube", "666fc", "e-c-corinthi-ns", "mpupo110-fc");
$.each(times , function(key, val) {
$.ajax({
type: 'GET',
url: 'https://api.cartolafc.globo.com/time/' + val,
format: 'json',
error: function () {
alert('Ixi parça! Ocorreu um erro ao exibir as parciais, aguarde alguns segundos');
},
dataType: 'json',
success: function (data) {
result.push({
nome_time: data.time.nome,
nome_cartola: data.time.cartola,
escudo: data.time.url_escudo_png
});
console.log(result);
}
});
});
});
if i put console.log(result); outsite the success of the ajax i just receive [] on the console
I would like the following result
times:[0]
{name:name, escudo:escudo, ...},
[1]
{name:name, escudo:escudo, ...},
Add
var x = {times : result}
JSON.stringify(x);
after $.each(times , function(key, val){}
It's returning []
because all of the $.ajax are running in parallel.
Using $.when.apply()
and passing all of the requests in, essentially awaits all requests, and then runs console.log(result)
$(document).ready(function() { var result = []; var times = new Array("sport-clube-balao", "verdao-fcsp", "kibeb-s-fc", "maria-fumaca-futebol-clube", "skolusa-fc", "fc-chikungunya", "furia-verde-1988-fc", "deixaa-queto-fc", "peixeraum", "espinoza-fc", "se-palestra-parente", "ca7fc", "lebucchi-fc", "impactos-fc", "tricolor-futebol-amigos", "augustinho-s-fs", "os-cara-de-egua", "porcos-locos-fc", "toniolotricolor-fc", "massa-clube", "made-china", "pipoca-jacare", "deprimeira-fc", "rocca-uryntians", "hu100-fc", "gabrukas-tln", "zaca-clube", "poderoso-rorinthians", "kbca-futebol-clube", "666fc", "ec-corinthi-ns", "mpupo110-fc"); var requests = []; $.each(times, function(key, val) { requests.push($.ajax({ type: 'GET', url: 'https://api.cartolafc.globo.com/time/' + val, format: 'json', error: function() { alert('Ixi parça! Ocorreu um erro ao exibir as parciais, aguarde alguns segundos'); }, dataType: 'json', success: function(data) { result.push({ nome_time: data.time.nome, nome_cartola: data.time.cartola, escudo: data.time.url_escudo_png }); } })); }); $.when.apply(null, requests).done(function() { console.log(result); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.