I have a complicated function in which 3 different AJAX requests are nested within each other, like so:
- function doAjax(strRedirect) {
$.ajax({
url: '/url1',
type: 'post',
data: datastring1,
success: function() {
$.ajax({
url: '/url2',
type: 'post',
data: datastring2,
success: function() {
alert('a - ' + strRedirect);
$.ajax({
url: '/url3',
success: function(data) {
alert('b - ' + strRedirect);
}
});
}
});
}
});
}
All of this works fine, except that the 2 alerts are bringing back "a - /contactus" and "undefined".
How on Earth is the strRedirect variable losing its scope? It keeps its scope all the way until the last AJAX call. It makes no sense to me at all. Is there any scenario where the variable might lose its scope? Perhaps if the last AJAX call takes a really long time to complete? Any help would be very much appreciated because I'm tearing my hair out over here.