sto effettuare le chiamate API multiple, dopo di che voglio caricare i risultati combinati di ogni chiamata:
$.when(
$.get(localAPI, data, function(response) {
globalStore.localShares = Number(response);
}),
$.get(facebookAPI, '', function(response){
globalStore.facebookShares = Number(response[0].share_count);
}),
$.getJSON(pinterestAPI, {url: url}).done(function(response){
globalStore.pinterestShares = Number(response.count);
})
).always(function(){
//Do stuff
});
Se le chiamate non riescono $.get
, la funzione di callback $.always
viene ancora eseguita.
Ma
Se solo $.get
chiamata non riesce, nega le azioni dei precedenti bandi.
Quindi, se la prima chiamata fallisce, globalStore
restituisce con due voci. Se la prima chiamata ha esito positivo ma il secondo non riesce, globalStore
restituisce con un solo elemento. E se le prime due chiamate hanno esito positivo ma l'ultimo non riesce, globalStore
restituisce vuoto.
C'è qualche modo per aggirare questo?
Edit:
Sì, ho cercato di gestire guasti entro $.when
come questo:
$.when(
$.get(mu30_ajax_frontend.ajaxurl, data, function(response) {
globalStore.localShares = Number(response);
}).fail(function(){
globalStore.localShares = 0;
}),
$.get(facebookAPI, '', function(response){
globalStore.facebookShares = Number(response[0].share_count);
}).fail(function(){
globalStore.facebookShares = 0;
}),
$.getJSON(pinterestAPI, {url: url}).done(function(response){
globalStore.pinterestShares = Number(response.count);
}).fail(function(){
globalStore.pinterestShares = 0;
})
).always(function(){
//Do stuff
});
Ma ottengo lo stesso risultato.
Hai provato a gestire i guasti? '$ .get(). fail (function (err) {})' –
Sì, l'ho appena provato. Ho aggiunto il codice sopra. Ancora ottenere lo stesso esatto problema. – ibhhvc
Sembra un duplicato di [$ .Deferred: Come rilevare quando è stata eseguita ogni promessa] (http://stackoverflow.com/q/19177087/1048572), ma non sono sicuro di quale comportamento esatto ci si aspetta dal tuo "modo di aggirare". – Bergi