$.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都需要處理錯誤消息。