Making repeatedly calls to async methods can be a nightmare. In this case, it makes sense to use 2 ajax methods, instead of one.
Here is a simple solution to overcome this problem. See that ajaxcalls is emptied after the success response for the first ajax call and then the second ajax method is used to make one single call to the async action method.
Hope it helps.
View:
Controller:
Here is a simple solution to overcome this problem. See that ajaxcalls is emptied after the success response for the first ajax call and then the second ajax method is used to make one single call to the async action method.
Hope it helps.
View:
@section Scripts{
<script type="text/javascript">
var smartDebitObject = new Object();
smartDebitObject.MembershipNumber = $("#MembershipNumber").val();
smartDebitObject.ProfileId = $("#ProfileId").val();
smartDebitObject.FirstName = $("#FirstName").val();
smartDebitObject.LastName = $("#LastName").val();
smartDebitObject.AddressLine1 = $("#AddressLine1").val();
smartDebitObject.Postcode = $("#Postcode").val();
smartDebitObject.Town = $("#Town").val();
smartDebitObject.FrequencyType = $("#FrequencyType").val();
var request = JSON.stringify(smartDebitObject);
function UpdateCRMForSmartDebitDDI() {
$.ajax({
url: '@Url.Action("UpdateCRMAfterSmartDebitDDICreation", "ECommerce")',
dataType: "json",
type: "POST",
contentType: 'application/json; charset=utf-8',
data: request,
async: true,
processData: false,
cache: false,
success: function (jsonResult) {
debugger;
if (jsonResult.Message === 'SUCCESS') {
// show the "Confirm purchase" button!
document.getElementById('divSmartDebitCallbackPartialConfirmButton').style.display = "block";
// set the orderId
document.getElementById('OrderId').value = jsonResult.Guid;
if (data.Exception === 'CRM_UPDATE_FAILURE') {
document.getElementById('ExceptionMessage').value = jsonResult.Exception;
}
alert("jsonResult.retVal:
" + jsonResult.Message + "jsonResult.Guid:
" + jsonResult.Guid + "
jsonResult.Exception: " + jsonResult.Exception);
}
},
error: function (xhr) {
// An error occured, nothing to do!
}
});
}
$(document).ready(function () {
var ajaxcalls = [];
var successfullCallback = false;
var monitor = setInterval(function () {
var ajaxcall = $.ajax({
url: '@Url.Action("GetSmartDebitCachedCallbackResult","ECommerce", new {
membershipNumber= Model.MembershipNumber })',
dataType: "json",
type: "GET",
contentType: 'application/json; charset=utf-8',
async: true,
processData: false,
cache: false,
success: function (jsonResult) {
if (jsonResult.Message === 'SUCCESS') {
clearInterval(monitor);
ajaxcalls.forEach(function (item) {
// abort all ajax calls
item.abort();
}, this);
if (!successfullCallback) {
successfullCallback = true;
// update crm now!
UpdateCRMForSmartDebitDDI();
}
}
},
error: function (xhr) {
// An error occured, nothing to do!
}
});
ajaxcalls.push(ajaxcall);
}, 2000);
});
</script>
Controller:
[HttpGet]
public JsonResult GetSmartDebitCachedCallbackResult(string membershipNumber)
{
var retValSmartDebitCallbackResult = CacheHandler.GetValue(membershipNumber) as string;
var jsonResult = new Result();
if (retValSmartDebitCallbackResult == Constants.CacheKeysResult.SUCCESS)
{
jsonResult.Message = Constants.CacheKeysResult.SUCCESS;
}
return Json(jsonResult, JsonRequestBehavior.AllowGet);
}
[HttpPost]
public async Task<JsonResult> UpdateCRMAfterSmartDebitDDICreation(SmartDebitViewModel request)
{
var jsonResult = new Result();
await FinalSmartDebitProcessAsync(request, jsonResult);
return Json(jsonResult, JsonRequestBehavior.AllowGet);
}
Comments
Post a Comment