javascript - 处理 jquery ajax 错误

标签 javascript asp.net-mvc jquery asp.net-mvc-4

在我的 MVC 布局页面中,我有以下内容:

$("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 调用出错 - 在本例中这是正确的。但它确实带我到/Index/Error 页面,这是不正确的。我希望“$("body").ajaxError”能够处理网站上的所有 ajax 错误,除了我调用 refreshActionLinks 的页面。注意,我的 ajax 调用周围已经有 try/catch。为什么那行不通?

谢谢

最佳答案

想通了:

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();
    } 
}

关于javascript - 处理 jquery ajax 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15379631/

相关文章:

javascript - 每列的总和不基于 jQuery 中的类

javascript - jquery Sticky Panel-v1.4.1 - iphone、ipad 修复吗?

javascript - 有没有办法在 asp.net mvc 3 中使用 JavaScript、Jquery 监听事件

javascript - 在 Javascript 中处理多行 HTML

javascript - 将 src 属性的特定部分替换为 iframe 元素

javascript - 如何在我的 iframe 加载时向我的页面添加加载指示器?

javascript - 我如何使用 jquery 在文本字段中添加不同的字体颜色

javascript - 从 React Admin 中的同一端点创建多个资源以应用不同的过滤器

asp.net-mvc - 注入(inject) AutoMapper 实例

javascript - Angular : force resolve again