Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery how to use multiple ajax calls one after the end of the other

I am in mobile app and I use multiple Ajax calls to receive data from web server like below

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         });     }); } 

How can I force the 2nd ajax call to begin after the end of the first... the 3rd after the end of the 2nd and so go on?

like image 885
kosbou Avatar asked Feb 10 '12 21:02

kosbou


People also ask

How do I handle multiple Ajax requests?

The jQuery library provides a way to make any callback function waiting for multiple Ajax calls. This method is based on an object called Deferred. A Deferred object can register callback functions based on whether the Deferrred object is resolved or rejected. Here is an example of the Deferred object.

Can we execute run multiple Ajax request simultaneously in JQuery?

To iterate through the response, there is a callback function attached to it. This callback function gets executed once both the Ajax requests are finished. In case where multiple Deferred objects are passed to $. when() , it takes the response returned by both calls, and constructs a new promise object.

How can stop multiple Ajax calls in JQuery?

for prevent multiple ajax request in whole site. For example: If use ajax request in other ajax page, Using ajax in php loop, etc, Give you multiple ajax request with one result. I have solution: Use window.


1 Answers

Place them inside of the success: of the one it relies on.

$.ajax({     url: 'http://www.xxxxxxxxxxxxx',     data: {name: 'xxxxxx'},     dataType: 'jsonp',     success: function(data){          // do stuff          // call next ajax function         $.ajax({ xxx });     } }); 
like image 149
Timothy Aaron Avatar answered Oct 08 '22 07:10

Timothy Aaron