You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET MVC中使用jQuery AJAX删除数据报错:资源未找到

解决ASP.NET MVC中jQuery AJAX删除记录时的“资源未找到”问题

你遇到的“资源未找到”错误主要来自几个代码细节问题,我帮你逐一排查并修复:

1. AJAX请求URL拼接错误

你的JS代码里URL拼接少了斜杠,导致生成的URL是/Category/Delete8而不是正确的/Category/Delete/8,路由无法匹配到你的Delete方法。

修复Category.js的URL部分:

function Delete(id) {
    var ans = confirm("Are you sure you want to delete this Record?");
    if (ans) {
        // 手动添加斜杠,生成正确的路由格式
        var deleteUrl = "/Category/Delete/" + id;
        $.ajax({
            type: "POST",
            url: deleteUrl,
            success: function (response) {
                alert("Successfully Deleted");
                window.location.href = response;
            },
            error: function(xhr, status, error) {
                // 添加错误提示,方便调试问题
                alert("Error: " + xhr.responseText);
            }
        });
    }
    // 阻止后续默认跳转行为
    return false;
}

2. 缺少AntiForgeryToken验证

你的控制器方法加了[ValidateAntiForgeryToken]特性,但AJAX请求里没有携带这个令牌,就算URL正确,请求也会被拒绝(后续会触发400错误)。

步骤1:在Index.cshtml中添加AntiForgeryToken
在页面任意位置(比如表单内或页面顶部)添加:

@Html.AntiForgeryToken()

步骤2:在AJAX请求中携带令牌
修改Category.js的AJAX配置,添加data参数传递令牌:

function Delete(id) {
    var ans = confirm("Are you sure you want to delete this Record?");
    if (ans) {
        var deleteUrl = "/Category/Delete/" + id;
        // 获取页面上的AntiForgeryToken值
        var token = $('input[name="__RequestVerificationToken"]').val();
        $.ajax({
            type: "POST",
            url: deleteUrl,
            data: { 
                __RequestVerificationToken: token,
                id: id // 确保id参数正确传递,路由已捕获时可省略,但带上更保险
            },
            success: function (response) {
                alert("Successfully Deleted");
                window.location.href = response;
            },
            error: function(xhr, status, error) {
                alert("Error: " + xhr.responseText);
            }
        });
    }
    return false;
}

3. 阻止ActionLink的默认GET请求

你的@Html.ActionLink生成的是一个跳转链接,点击时会默认发起GET请求到/Category/Delete/8,但你的控制器只有POST方法,必须阻止这个默认行为。

修改Index.cshtml的onclick属性:

<td>
    @Html.ActionLink("Edit", "Update", new { id = category.CategoryID }, new { @class = "btn btn-warning" })
    @Html.ActionLink("Delete", "Delete", new { id = category.CategoryID }, new { @class = "btn btn-danger", @onclick = "return Delete('" + category.CategoryID + "');" })
</td>

这里添加了return,让ActionLink的默认跳转被Delete函数的return false阻止。

额外优化建议

  • 避免硬编码URL:可以在视图里直接生成正确的Delete URL,传递给JS函数,避免路由规则变化时出错:
@Html.ActionLink("Delete", "Delete", new { id = category.CategoryID }, new { @class = "btn btn-danger", @onclick = "return Delete('@Url.Action("Delete", "Category", new { id = category.CategoryID })');" })

然后JS里直接使用这个URL:

function Delete(deleteUrl) {
    var ans = confirm("Are you sure you want to delete this Record?");
    if (ans) {
        var token = $('input[name="__RequestVerificationToken"]').val();
        $.ajax({
            type: "POST",
            url: deleteUrl,
            data: { __RequestVerificationToken: token },
            success: function (response) {
                alert("Successfully Deleted");
                window.location.href = response;
            },
            error: function(xhr, status, error) {
                alert("Error: " + xhr.responseText);
            }
        });
    }
    return false;
}
  • 用button代替ActionLink:彻底避免默认GET请求的问题,代码更直观:
<button class="btn btn-danger" onclick="return Delete('@Url.Action("Delete", "Category", new { id = category.CategoryID })');">Delete</button>

验证修改后的流程

  1. 点击Delete按钮,弹出确认提示框
  2. 确认后,AJAX发送带AntiForgeryToken的POST请求到正确URL
  3. 控制器执行删除操作,返回Index页面的URL
  4. 前端自动跳转到Index页面,完成删除流程

内容的提问来源于stack exchange,提问作者Kasun Subhashana

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 11:52:36