share
Stack OverflowHow can I do API Call after different APi Call is finished?
[0] [1] Artur Stary
[2012-08-02 10:45:25]
[ javascript jquery ajax callback jsonp ]
[ https://stackoverflow.com/questions/11775841/how-can-i-do-api-call-after-different-api-call-is-finished ]

Go to http://starodaj.eu/apitest/index2.html to see my code

Click "Show Availability". It should populate inputs. I have a problme (probably with asynchronous) so I can't populate everything by 1 click. When I click 'ShowAvailability' more times - everything work fine. How can I fix that?

    function callAPI(yourAPIKey){
    var enquiry = "http://api.roomex.com/api/hotel?apiKey=" + yourAPIKey;
     //alert(enquiry);
     $.ajax({
         url: enquiry,
         type: 'GET',
         dataType: "jsonp",
         jsonp: "callback",
         jsonpCallback: "jsonpCallback2",
         complete: function (response, responseCode) {
         },
         success: function (json) {
             $.each(json.Contracts, function (index, contract) {
                // alert("Count before increament : " + Count);
                // alert(contract.ContractCode);
                 ContractsArray[Count] = contract.ContractCode;

                // alert("Count after increament : " + Count);
                // alert("ContractsArray[Count]: " + ContractsArray[Count]);
                 Count++;

             });
             for(var i = 0; i < Count; i++){
                 //alert("ContractsArray[" + i + "]: " + ContractsArray[i]);
                 getAvailability(yourAPIKey, ContractsArray[i], startDate, endDate);
                 getRates(yourAPIKey, ContractsArray[i], startDate, endDate);
                 //alert("Finish of ContractsArray[" + i + "]: " + ContractsArray[i]);
             }
         }
     });
    }
[+1] [2012-08-02 11:06:21] Roest [ACCEPTED]

Your script produces errors with these lines

jsonpCallback: "jsonpCallback3",
jsonpCallback: "jsonpCallback",
jsonpCallback: "jsonpCallback2",

when I removed them it just fills the entire table


Well apparently those lines are not needed so leave them out or you could define these callbacks. - Roest
1