Search code examples
javascriptjqueryajaxbackbone.jsresponse

how to manage ajax response errors


I am writing javascript app and I am using backbone.js.on the app i have several views in a page, majority views sent request (requests <- they are ajax-query,but not backbone.js ajax-query) to server at the same time, and they had a result in a closely period. If something happened wrong with server or net some response would broken and returned with errors. The problem is that all errors need to display by alerts to user, despite the fact that it is boring to see more and more alerts for users . How can I manage the alerts? I used settimeout to show alerts, but this way is not helpful to decrease count of alerts. If You know another way please tell me about it ?


Solution

  •     function alertMajorError(errMsg) {
            errMsg = typeof errMsg !== 'undefined' ? errMsg : 'Fatal Error!';
            var n = noty({layout:'center',type:'error',text:errMsg,timeout:false});
        }
        $(document).ajaxError(function(event, jqxhr, settings, exception) {
            if (exception == 'abort') return false;
            alertMajorError('An AJAX error [' + exception + '] occurred in script: ' + settings.url + '<br \><br \>Please report this to the development team!');
            console.log(event, jqxhr, settings, exception);
    });
    

    You can set up a default AJAX error reporter in your main JS file like I have. I use the jQuery noty plugin!