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都需要處理錯誤消息。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章