2013-03-15 96 views
0

从请求到服务器我返回401错误。为什么ajax安装在这种情况下不起作用?

我想处理这个并适当地重定向用户。

起初我试图用ajaxSetup来捕获错误并处理它,但是这并没有被打到。

然后我改变它使用ajaxError方法,这个工程。

为什么当我使用ajaxSetup时不会碰到错误方法?

这工作:

$("body").ajaxError(
       function (e, request) { 
        if (request.status == 401) { 
         alert("Your session has expired. Please login again to continue."); 
         window.location = "/admin/account/logon"; 
        } 
       } 
      ); 

但是,这并不工作:

$.ajaxSetup({ 
     cache: false, 
     error: function (jqXHR, textStatus, errorThrow) { 
      if (jqXHR.status == 401) { 
       alert("Your session has expired. Please login again to continue."); 
       window.location = "/admin/account/logon"; 
      } 
     } 
    }); 

它使用jQuery不显眼的Ajax触发,链接如下:

<a title="Delete this comment" class="close " data-ajax="true" data-ajax-confirm="Are you sure you want to delete this comment?" data-ajax-method="POST" data-ajax-mode="replace" data-ajax-update="#Comments" href="/myurl">×</a> 
+0

使用$(document)而不是$(body)。另外,我不明白这个问题,您是否问过_why_jQuery语法是这样工作的?你的ajax调用中的 – 2013-03-15 04:22:21

+0

是否注册了更多的错误处理程序?分享你如何使ajax呼叫 – 2013-03-15 04:23:46

+0

我已经更新了问题和其他细节。 – shenku 2013-03-15 04:29:51

回答

3

From the documentation

Note: 
Global callback functions should be set with their respective global Ajax event handler methods— 
.ajaxStart(), 
.ajaxStop(), 
.ajaxComplete(), 
.ajaxError(), 
.ajaxSuccess(), 
.ajaxSend() 

—rather than within the options object for $.ajaxSetup(). 
+1

+1另外,我发现这张票:http://aspnet.codeplex.com/workitem/9742 – 2013-03-15 04:36:58

相关问题