jQuery 删除(RESTful)

标签 jquery asp.net-mvc rest

我正在尝试通过 Ajax 执行 DELETE(首选)或 POST。

$(document).ready(function () {
    $("a.deleteOne").click(function () {
        var id = $(this).parents('td:first').children('.hiddenId').val();
        var title = $(this).parents('tr:first').children('td:first').text();
        if (confirm("Really delete the [" + title + "] document?")) {
            $.ajax({
                type: "POST",
                url: "/WebApp/Area/Documents/Delete/" + id,
                //data: { id: id },
                success: function () { alert("Document [" + title + "] deleted."); },
                failure: function () { alert("Document [" + title + "] was NOT deleted."); }
            });
        }
    });
    return false;
});

由于某种原因, Controller 的操作没有被调用。

Action code looks like:
[ValidateAntiForgeryToken]
[Transaction]
[AcceptVerbs(HttpVerbs.Post)]
//[AcceptVerbs(HttpVerbs.Delete)]
public ActionResult Delete(int id)
{
    if (!BaseAuthorizationProvider.HasPermissionToPerform(App.Core.Security.Operations.CreateDocumentTask))
    {
        HandlePermissionException();
    }
    ActionConfirmation deleteConfirmation = DocumentManagementService.Delete(id);
    TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = deleteConfirmation.Message;
    return RedirectToAction("Index");
}

我错过了什么?

最佳答案

试试这个,略有不同,我猜你正在使用区域,只需替换 YourController 和 YourArea 即可。

$.ajax({
    type: "POST",
    url: '@Url.Action("Delete", "YourController", new { area = "YourArea" })',
    data: { id: id },
    success: function () { alert("Document [" + title + "] deleted."); },
    error: function () { alert("Document [" + title + "] was NOT deleted."); }
});

关于jQuery 删除(RESTful),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14370023/

相关文章:

javascript - jquery 调用不会在更新面板中触发

asp.net-mvc - 模拟 FormsAuthentication.Authenticate() 方法

spring - 使用 spring 和 restful webservice 自定义 HTTP 状态代码

json - Servant-server 的自定义 JSON 错误

javascript - 将类更改为元素不会更改背景颜色

javascript - 了解用户正在 slimbox2.js 中查看的图像

java - Extjs 无法读取属性错误

c# - 不包含 'Contains' 的定义

c# - 从 MVC 4 ASP.NET 应用程序发送电子邮件

ruby-on-rails - 如何为静态资源创建 "edit"url?