Skip to content

Instantly share code, notes, and snippets.

@Andaeiii
Created June 1, 2022 11:48
Show Gist options
  • Save Andaeiii/da31f210a8084a66b15698818c1d6e8b to your computer and use it in GitHub Desktop.
Save Andaeiii/da31f210a8084a66b15698818c1d6e8b to your computer and use it in GitHub Desktop.
Calling multiple ajax calls all at once using JQuery..
//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