2013-04-18 61 views
0

我想在發生錯誤時添加(默認)的行動,它不是(403,500或410碼):

$.ajaxSetup({ 
     statusCode: { 
      403: function() { 
       window.location = '@Url.Action("LogOn", "Account", new {area = "", msg = "forbidden", returnUrl = HttpContext.Current.Request.RawUrl})' + window.location.hash; 
      }, 
      500: function() { 
       window.location = '@Url.Action("AccessDenied", "Error")'; 
      }, 
      410: function() { 
       window.location = '@Url.Action("Deleted", "Error")'; 
      } 
      // ANY OTHER ERROR CODE - but it doesn't work, how can i do it? 
      if not any above and it's an error then => 
      window.location = '@Url.Action("Index", "Error")'; 
     } 
    }); 

回答

2

您應該使用ajaxError()處理錯誤ajaxSetup()

$(document).ajaxError(function(event, xhr, options) { 
    switch (xhr.status) 
    { 
     case 403: 
      window.location = '@Url.Action("LogOn", "Account", new {area = "", msg = "forbidden", returnUrl = HttpContext.Current.Request.RawUrl})' + window.location.hash; 
     break; 
     case 500: 
      window.location = '@Url.Action("AccessDenied", "Error")'; 
     break; 
     case 410: 
      window.location = '@Url.Action("Deleted", "Error")'; 
     break; 
     default: 
      window.location = '@Url.Action("Index", "Error")'; 
     break; 
    } 
    } 
}); 
+0

但使用ajaxError一些工作,原因打的功能:請參閱:HTTP: //stackoverflow.com/questions/16080462/handling-session-timeout-in-ajax – ShaneKm

+0

@ShaneKm檢查我的更新編輯實際參數 – mattytommo

+0

,但仍然。由於某種原因ajaxError從未被擊中。 http://stackoverflow.com/questions/16080462/handling-session-timeout-in-ajax – ShaneKm

0

的 「完整」 事件總是被解僱。

complete: function(jqXHR, textStatus) { 
    switch (jqXHR.status) { 
     case 200: 
      alert("error 200"); 
      break; 
     case 404: 
      alert("error 404"); 
      break; 
     default: 
      alert("DEFAULT"); 
    } 
} 
+0

@Shane Km是它ü – PSR