Our app handles all users information via AJAX using jQuery. We return data.success or data.error depending on if it the API works or not. We also run the jQuery error() function on each post() just in case there is an actual problem reaching the server. It’s getting tedious having the same thing for all of them.
Here’s a simplified example:
$.post('/api/nodeSave.php', {
net: true
}, function(data) {
if (data.error) {
mainAlert('error',data.error);
} else if (data.success) {
mainAlert('success',data.success);
}
})
.error(function(data) {
mainAlert('error','There was a problem contacting the server.');
});
My question is: is there any way to add the trailing error() function as a default to all post() functions we run so I don’t have to include it every single time?
I disagree with @Kevin :
Note: Global callback functions should be set with their respective global Ajax event handler methods—.ajaxStart(), .ajaxStop(), .ajaxComplete(), .ajaxError(), .ajaxSuccess(), .ajaxSend()—rather than within the options object for $.ajaxSetup().