javascript
  • asp.net-mvc
  • jquery
  • asp.net-mvc-4
  • 2013-03-13 63 views 2 likes 
    2

    在我的MVC页面布局我有以下几点:手柄的jQuery Ajax错误

    $("body").ajaxError(
        function (e, request) { 
         if (request.status == 403 || request.status == 500) { 
          window.location = '@Url.Action("LogOn", "Account", new {area = "", msg = "forbidden", returnUrl = HttpContext.Current.Request.RawUrl})' + window.location.hash; 
         return; 
         } 
         window.location = '@Url.Action("Index", "Error")'; 
        } 
    ); 
    

    另一页上,我像这样进行Ajax调用:

    ... 
           $.when(refreshActionLinks(row, machineId, packageId)).done(function(a1) { 
            row.find("span").text(opStatus).removeClass("pending"); 
            progressbar.progressbar("destroy"); 
            $(row).flash(bg[1], 1000); 
           }); 
    ... 
    

    javascript函数:

    function refreshActionLinks($row, machineId, packageId) { 
        try { 
         var json = JSON.stringify({ packageId: packageId, machineId: machineId, tabType: $("#TabType").val() }); 
         console.log("refreshActionLinks => " + json); 
         $row.find("td.options div.actionLinks").html("<img src='@Url.Content("~/Content/images/ajax-load2.gif")' />"); // pending 
         return $.ajax({ 
          url: "@Url.Action("GetActionLinks", "Packages")", 
          data: json, 
          timeout: 50000, 
          contentType: 'application/json', 
          type: 'POST', 
          success: function (data) { 
           if ($row.length) { 
            $row.find("td.options div.actionLinks").html(data); 
           } 
          }, 
          error: function(jqXHR, textStatus, errorThrown) { 
           console.log(textStatus, errorThrown); 
          } 
         }); 
        } catch(e) { 
         // hide icons 
         $row.find("a.action").remove(); 
        } 
    } 
    

    问题是,虽然refreshAction函数正在执行,但单击菜单链接会导致ajax调用错误 - 这是在这种情况下是正确的。但它确实带我到/索引/错误页面,这是不正确的。我想要“$(”body“)。ajaxError”来处理网站上的所有ajax错误,除了我调用refreshActionLinks的页面外。注意,我已经有了围绕我的ajax调用的try/catch。为什么不工作?

    感谢

    回答

    0

    想通了:

    AJAX有一个设置:

    global: false 
    

    现在我的功能看起来是这样的:

    function refreshActionLinks($row, machineId, packageId) { 
        try { 
         var json = JSON.stringify({ packageId: packageId, machineId: machineId, tabType: $("#TabType").val() }); 
         console.log("refreshActionLinks => " + json); 
         $row.find("td.options div.actionLinks").html("<img src='@Url.Content("~/Content/images/ajax-load2.gif")' />"); // pending 
         return $.ajax({ 
          url: "@Url.Action("GetActionLinks", "Packages")", 
          global: false, // disable error pages on failed ajax calls 
          data: json, 
          timeout: 50000, 
          contentType: 'application/json', 
          type: 'POST', 
          success: function (data) { 
           if ($row.length) { 
            $row.find("td.options div.actionLinks").html(data); 
           } 
          } 
         }); 
        } catch(e) { 
         // hide icons 
         $row.find("a.action").remove(); 
        } 
    } 
    
    相关问题