2012-02-10 13 views
26

मैं मोबाइल एप्लिकेशन में हूँ के अंत के बाद एक कॉल करता है और मैं कई अजाक्स नीचेjQuery कैसे कई ajax उपयोग करने के लिए अन्य

function get_json() { 
    $(document).ready(function() { 
     $.ajax({ 
      url: 'http://www.xxxxxxxxxxxxx', 
      data: { 
       name: 'xxxxxx' 
      }, 
      dataType: 'jsonp', 
      //jsonp: 'callback', 
      //jsonpCallback: 'jsonpCallback', 
      success: function(data) { 
       $.each(data.posts, function(i, post) { 
        $.mobile.notesdb.transaction(function(t) { 
         t.executeSql('INSERT into bill (barcode, buildingcode, buildingaddress, flatname, flatdescription, entryseason, period, amount, pastpayments, todaypayments, paydate, receiptno) VALUES (?,?,?,?,?,?,?,?,?,?,?,?);', [post.Id, post.Code, post.Address, post.Name, post.Description, post.EntrySeason, post.Period, post.Revenue, post.PastPayments, post.todaypayments, post.paydate, post.receiptno], 
         //$.mobile.changePage('#page3', 'slide', false, true), 
         null); 
        }); 
        $('#mycontent').append(post.Name); 
       }); 
      } 
     }); 

     $.ajax({ 
      xxxx 
     }); 

     $.ajax({ 
      xxxx 
     }); 
    }); 
} 

की तरह वेब सर्वर से डेटा प्राप्त करने मैं 2 ajax कैसे मजबूर कर सकते हैं कॉल का उपयोग पहले के अंत के बाद शुरू करने के लिए कॉल करें ... दूसरे के अंत के बाद तीसरा और फिर आगे बढ़ें?

+0

इसके बारे में एकमात्र चीज एक असफल AJAX कॉल के परिणामस्वरूप कोई अन्य AJAX कॉल नहीं किया जाएगा (क्योंकि अगर ऐसा नहीं होता है तो "AJAX कॉल विफल होने पर ऐसा नहीं होता है", यह हमेशा के लिए "सफलता" की प्रतीक्षा करेगा)। शायद यह वही है जो आप चाहते हैं ... बस कुछ विचार करने के लिए। –

+0

मुझे लगता है कि आप $L का उपयोग बंद कर देंगे। जब @Lyon सुझाव देता है। –

उत्तर

31

उन पर निर्भर करता है success: के अंदर उन्हें रखें।

एक नामित समारोह में
$.ajax({ 
    url: 'http://www.xxxxxxxxxxxxx', 
    data: {name: 'xxxxxx'}, 
    dataType: 'jsonp', 
    success: function(data){ 

     // do stuff 

     // call next ajax function 
     $.ajax({ xxx }); 
    } 
}); 
+0

मैं यह कैसे कर सकता हूं आप इसे समझा सकते हैं – kosbou

+0

'$ .ajax()' कॉल के कई स्तरों के लिए दोहराएं। –

+0

आप अज्ञात संख्या में AJAX अनुरोधों के लिए इसे फिर से कर सकते हैं। वर्तमान में प्रश्न के कुछ जवाब हैं जो इसे प्रदर्शित करते हैं। – Jasper

8

लपेटें प्रत्येक ajax कॉल और सिर्फ उन्हें पिछले कॉल की सफलता कॉलबैक करने के लिए जोड़ें:

function callA() { 
    $.ajax({ 
    ... 
    success: function() { 
     //do stuff 
     callB(); 
    } 
    }); 
} 

function callB() { 
    $.ajax({ 
    ... 
    success: function() { 
     //do stuff 
     callC(); 
    } 
    }); 
} 

function callC() { 
    $.ajax({ 
    ... 
    }); 
} 


callA(); 
+0

thx ...और आपके त्वरित उत्तर – kosbou

+1

के लिए यह फ़ंक्शन नाम भ्रामक बनाता है: कॉल ए न केवल कॉल ए है, बल्कि कॉलब और सी – marstone

42

आप कुछ हद तक करीब हैं, लेकिन आप document.ready घटना के अंदर अपने समारोह रखना चाहिए दूसरी तरफ के बजाय हैंडलर।

एक समारोह में अपने AJAX कॉल रखो और यह AJAX कॉलबैक से फोन:

$(function() { 

    //setup an array of AJAX options, each object is an index that will specify information for a single AJAX request 
    var ajaxes = [{ url : '<url>', dataType : 'json' }, { url : '<url2>', dataType : 'xml' }], 
     current = 0; 

    //declare your function to run AJAX requests 
    function do_ajax() { 

     //check to make sure there are more requests to make 
     if (current < ajaxes.length) { 

      //make the AJAX request with the given data from the `ajaxes` array of objects 
      $.ajax({ 
       url  : ajaxes[current].url, 
       dataType : ajaxes[current].dataType, 
       success : function (serverResponse) { 
        ... 
        //increment the `current` counter and recursively call this function again 
        current++; 
        do_ajax(); 
       } 
      }); 
     } 
    } 

    //run the AJAX function for the first time once `document.ready` fires 
    do_ajax(); 
}); 
+2

यह एक चिह्नित उत्तर होना चाहिए। बहुत अच्छा। – sohtimsso1970

+1

पवित्र अजाक्सता, बैटमैन! यह निश्चित रूप से ओपी की जरूरतों पर एक जटिलता है। @ ल्योन का जवाब बहुत अच्छा है। – igorsantos07

0
$(document).ready(function(){ 
$('#category').change(function(){ 
    $("#app").fadeOut(); 
$.ajax({ 
type: "POST", 
url: "themes/ajax.php", 
data: "cat="+$(this).val(), 
cache: false, 
success: function(msg) 
    { 
    $('#app').fadeIn().html(msg); 
    $('#app').change(function(){  
    $("#store").fadeOut(); 
     $.ajax({ 
     type: "POST", 
     url: "themes/ajax.php", 
     data: "app="+$(this).val(), 
     cache: false, 
     success: function(ms) 
      { 
      $('#store').fadeIn().html(ms); 

      } 
      });// second ajAx 
     });// second on change 


    }// first ajAx sucess 
    });// firs ajAx 
});// firs on change 

}); 
6

तुम भी jQuery जब और फिर कार्यों का उपयोग कर सकते हैं। उदाहरण के लिए

$.when($.ajax("test.aspx")).then(function(data, textStatus, jqXHR) { 
    //another ajax call 
}); 

https://api.jquery.com/jQuery.when/

+2

पदानुक्रम के 4 स्तरों के लिए कैसे लिखना है? –

+0

प्रदर्शन की गति के बारे में कैसे? –

1

मैं के बाद से यह ajax कॉल क्रम नहीं है लेकिन वह निश्चित रूप से स्वाद की बात है और अधिक व्यावहारिक होने के लिए निम्नलिखित पर विचार करें।

function check_ajax_call_count() 
{ 
    if (window.ajax_call_count==window.ajax_calls_completed) 
    { 
     // do whatever needs to be done after the last ajax call finished 
    } 
} 
window.ajax_call_count = 0; 
window.ajax_calls_completed = 10; 
setInterval(check_ajax_call_count,100); 

अब आप अपने ajax अनुरोध की सफलता हिस्से के अंदर window.ajax_call_count पुनरावृति कर सकते हैं जब तक यह कॉल की निर्धारित संख्या तक पहुँच जाता भेजें (window.ajax_calls_completed)।

4

यह एक सबसे सुंदर समाधान है जिसे मैं थोड़ी देर के लिए उपयोग कर रहा हूं। इसे बाहरी काउंटर वैरिएबल की आवश्यकता नहीं है और यह अच्छी तरह से encapsulation प्रदान करता है।

var urls = ['http://..', 'http://..', ..]; 

function ajaxRequest (urls) { 
    if (urls.length > 0) { 
     $.ajax({ 
      method: 'GET', 
      url: urls.pop() 
     }) 
     .done(function (result)) { 
      ajaxRequest(urls); 
     }); 
    } 
} 

ajaxRequest(urls); 
-1

हम बस का उपयोग

async: false 

यह अपनी जरूरत क्या करेंगे कर सकते हैं।

+0

नहीं, यह सबसे बुरी चीज है जो आप कर सकते हैं क्योंकि यह आपके ब्राउज़र को तब तक लटकाएगा जब तक AJAX अनुरोध पूरा न हो जाए। –

संबंधित मुद्दे