मैं कई API कॉल बना रहा हूं, जिसके बाद मैं प्रत्येक कॉल के संयुक्त परिणाम लोड करना चाहते रद्द:
$.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
});
$.get
कॉल में विफल रहते हैं , $.always
कॉलबैक फ़ंक्शन अभी भी निष्पादित करता है।
लेकिन
सिर्फ एक $.get
कॉल विफल रहता है, यह पिछले कॉल की कार्रवाई को नकारता।
तो, यदि पहली कॉल विफल हो जाती है, तो globalStore
दो आइटमों के साथ लौटाता है। यदि पहली कॉल सफल होती है लेकिन दूसरा विफल रहता है, तो globalStore
केवल एक आइटम के साथ लौटाता है। और यदि पहले दो कॉल सफल होते हैं लेकिन अंतिम व्यक्ति विफल रहता है, तो globalStore
खाली हो जाता है।
क्या इसके आसपास कोई रास्ता है?
संपादित करें:
हाँ, मैं संभाल करने की कोशिश की है $.when
इस तरह के भीतर विफल रहता है:
$.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
});
लेकिन मैं एक ही परिणाम मिलता है।
क्या आपने असफलताओं को संभालने का प्रयास किया है? '$ .get()। असफल (फ़ंक्शन (गलती) {})' –
हां, मैंने अभी कोशिश की है। मैंने उपरोक्त कोड जोड़ा। अभी भी वही सटीक मुद्दा प्राप्त करें। – ibhhvc
यह $ $ के एक डुप्लिकेट की तरह लगता है। परिभाषित: प्रत्येक वादा निष्पादित होने पर कैसे पता लगाया जाए] (http://stackoverflow.com/q/19177087/1048572), लेकिन मुझे यकीन नहीं है कि आप किस सटीक व्यवहार की अपेक्षा करते हैं अपने "रास्ते के आसपास" से। मेरी पिछली टिप्पणी के अनुसार – Bergi