首页 > 技术文章 > Jquery 全局错误处理

zaodianshuo 2015-01-11 13:15 原文

$.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都需要处理错误消息。

推荐阅读