全局和特定错误处理AJAX jQuery

时间:2013-09-16 08:03:46

标签: javascript jquery backbone.js error-handling

我的全球处理如下。

$.ajaxSetup({
    statusCode: {

        200: function(res, status, xhr) {
        },
        500: function() {
        },

        400: function(jqXHR, textStatus, errorThrown) {
        },
});

我在骨干模型提取中进行具体处理,如下所示。

var xhr = byBackbone.fetch({//or save
    success: function(){}, 
    error: function(){
    if(xhr.statusCode == 400 && xhr.data == "some specific response"){
       //specefic handling
    } else {
      //how to call statusCode.400() here or how to leave it for getting handled globally?
    }
    }
});

所以在评论中,问题是,如何在捕获特定错误后将其留给全局处理?

1 个答案:

答案 0 :(得分:1)

可能 -

var cb = $.ajaxSetup({
    statusCode: {

        200: function(res, status, xhr) {
        },
        500: function() {
        },

        400: function() {
        }
}});

cb.statusCode[400](); // put this in your backbone ajax response handler

http://jsfiddle.net/hellomaya/rFhRW/1/