Created
June 1, 2022 11:48
-
-
Save Andaeiii/da31f210a8084a66b15698818c1d6e8b to your computer and use it in GitHub Desktop.
Calling multiple ajax calls all at once using JQuery..
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//spread the options here.... | |
let xhrURLOptns = { | |
cors: true, | |
crossDomain: true, | |
contentType: 'application/json', | |
withCredentials: true, | |
beforeSend: function (xhr) { | |
xhr.setRequestHeader('Authorization', `Bearer ${token}`); | |
} | |
}; | |
//then equate it in every request.... | |
const setkinStateCityAndLGA = (stateObj) => { | |
let state_id = $(stateObj).val(); | |
shwLdr('loading LGAs and Cities'); | |
$.when( | |
$.ajax({ //retrieve LGAS here.. | |
...xhrURLOptns, | |
url: `${prefix}api/v1/shared/GetAllLGAByStateCode?stateCode=${state_id}`, | |
type: 'get', | |
}), | |
$.ajax({ //retrieve Cities here.. | |
...xhrURLOptns, | |
url: `${prefix}api/v1/shared/GetCitiesByStateCode?stateCode=${state_id}`, | |
type: 'get', | |
}); | |
).done(function (lgas, cities) { | |
console.log(lgas, cities); //then here.. you treat seperately... | |
hideLdr(); | |
}).fail(function (xhr, status) { | |
console.log(xhr, status); | |
hideLdr(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment