I am using the following style of AJAX calls:
$.ajax({
dataType: "json",
type: "GET",
url: url
}).done(function(result, textStatus, jqXHR) {
if(result.success == 1) {
try {
if(!result.value_that_might_be_null) {
throw new Error("Value is null");
}
} catch(err) {
ajaxError(jqXHR, err.message);
}
} else {
throw new Error("Unsuccessful");
}
}).fail(ajaxError);
function ajaxError(jqXHR, textStatus, errorThrown) {
console.log('jqXHR: ' + jqXHR);
console.log('status: ' + textStatus);
console.log('error: ' + errorThrown);
}
This allows me to have good control over my AJAX calls and can test for certain values not being present, being set to the wrong thing etc.
But, I have a few different AJAX calls, each doing quite different things in the .done() callback, meaning they do need to be separate and not re-factored into a class.
But my problem is, how can I add extra parameters to ajaxError() when its called via the try/catch statement?
Can I just do something like:
function ajaxError(jqXHR, status, error, additionalParameter = null) {
//blah blah blah
}
ajaxError(jqXHR, errorMessage, errorThrown, myAdditionalParameter);