I am currently testing the feasibility of storing a reference to an ajax call upon failure and then retrying the call at a later time. My attempt so far has been the following:
$.ajax({
url: "controller/action",
data: {
params: "here"
}
})
.success(function (response) {
DoStuffHere();
})
.fail(function (error) {
ajaxRef = this;
DoOtherStuff();
})
Within the DoOtherStuff function, an attempt is made to make the ajax call using the stored reference:
function DoOtherStuff() {
if(ajaxRef)
{
$.ajax(ajaxRef);
}
}
This code is intentionally simplified to illustrate my objective.
It appears that the ajax call is indeed made, but the issue arises in maintaining a reference to the success or fail callbacks of the call. Consequently, when the call returns successfully, it does not execute DoStuffHere();
In my quest for a solution, I came across suggestions that indicate success by calling $.ajax(this)
within the success or fail functions. However, in my scenario, there are certain prerequisite functions and ajax calls that need to run before reattempting.
Is there a way to ensure that the success and fail callbacks are retained with the reference to the ajax call?