Global error handler for backbone.js ajax requests

前端 未结 4 2170
感动是毒
感动是毒 2020-12-23 13:02

Is there way to bind one error handler for ajax requests that performs by backbone.js?

My situation: I can get 401 (Unauthorized) at any time, so I need to show logi

4条回答
  •  滥情空心
    2020-12-23 13:29

    You can handle this via the jQuery .ajaxSetup method. We have an identical situation (backbone app which may get a 401 error at any time) and we handle it by using jQuery's ajaxSetup in the entry point of our app:

    var appView = new AppView(options);
    
    $.ajaxSetup({
        cache: false,
        statusCode: {
            401: function () {
                appView.signIn();
            }
        }
    });
    
    appView.render();
    
    Backbone.history.start({ root: config.rootUrl, pushState: true });
    

    This approach gives global error handling without the need to extend the Backbone base classes.

提交回复
热议问题