Jquery 全局错误处理

$.ajaxSetup({
    complete: function (request, status) {
        if (typeof (request) != 'undefined') {
            var responseText = request.getResponseHeader("X-Responded-JSON");
            if (responseText != null) {
                window.tipError('系统提示', '登录超时,请重新登录', null, null, function () {
                    window.location.href = window.location.href;
                });
            }
        }
    },
    error: function (jqXHR, textStatus, errorThrown) {
        var status = 0;
        switch (jqXHR.status) {
            case (500):
                //TODO 服务器系统内部错误
                status = 500;
                break;
            case (401):
                //TODO 未登录
                status = 401;
                break;
            case (403):
                //TODO 无权限执行此操作
                status = 403;
                break;
            case (408):
                //TODO 请求超时
                status = 408;
                break;
            case (0):
                //TODO cancel
                break;
            default:
                status = 1;
                //TODO 未知错误
        }
        if (status > 0)
            window.tipError('系统提示', '请联系网站管理员,错误代码:' + jqXHR.status, null, null, function () {
                window.location.href = window.location.href;
            });
    }
});

  





  不用每一次使用Ajax都需要处理错误消息。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章