I have a series of Ajax calls to call stored procedures and load my partial views. However, at times I get mixed results because the Ajax calls do not always fire in order. How can I ensure that they always fire as they are ordered in the click event?
$('#btnSearch').click(function (data) {
//Renders Plan Info
var theGroupNumber = $('#GroupNumber').val().substring($('#GroupNumber').val().length - 7).slice(0, 6);
var theStackCode = $('#StackCode').val();
var theEffectiveDate = $('#EffectiveDate').val();
//Clears the div Sections when the search button is clicked
$('#divPlanInfo').empty();
$('#divPrimaryStack').empty();
$('#divGlobalExceptions').empty();
$('#divPlanExceptions').empty();
$('#divGroupExceptions').empty();
// Renders Plan Info
$.ajax({
type: "POST",
url: '@Url.Action("PlanInfoView")', // the method we are calling
dataType: "HTML",
data: { GroupNumber: theGroupNumber, StackCode: theStackCode, EffectiveDate: theEffectiveDate },
success: function (data) {
$('#divPlanInfo').append(data);
},
error: function (result) {
alert('No Results Plan Info Found');
}
});
//Renders the Primary Stack Rule
$.ajax({
type: "POST",
url: '@Url.Action("PrimaryStackRuleView")', // the method we are calling
dataType: "html",
data: { GroupNumber: theGroupNumber, StackCode: theStackCode, EffectiveDate: theEffectiveDate },
success: function (data) {
if (data) {
$('#divPrimaryStack').append(data);
}
},
error: function (result) {
alert('No Primary Stack Rule Found');
}
});
//Renders the Global Exceptions
$.ajax({
type: "POST",
url: '@Url.Action("GlobalExceptionsView")', // the method we are calling
dataType: "html",
data: {},
success: function (data) {
if (data) {
$('#divGlobalExceptions').append(data);
}
},
error: function (result) {
alert('No Global Exceptions Found');
}
});
//Renders the Stack Code Exceptions
$.ajax({
type: "POST",
url: '@Url.Action("PlanExceptionsView")', // the method we are calling
dataType: "html",
data: { StackCode: theStackCode },
success: function (data) {
if (data) {
$('#divPlanExceptions').append(data);
}
},
error: function (result) {
alert('No Stack Code Exceptions Found');
}
});
//Renders the Group Number Exceptions
$.ajax({
type: "POST",
url: '@Url.Action("GroupExceptionsView")', // the method we are calling
dataType: "html",
data: { GroupNumber: theGroupNumber, StackCode: theStackCode },
success: function (data) {
if (data) {
$('#divGroupExceptions').append(data);
}
},
error: function (result) {
alert('No Stack Code Exceptions Found');
}
});
});