Keith Barrows
Keith Barrows

Reputation: 25308

jQuery DELETE (RESTful)

I am trying to do either a DELETE (preferred) or POST via Ajax.

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

For some reason the controller's action is not getting called.

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

What am I missing?

Upvotes: 2

Views: 230

Answers (3)

Keith Barrows
Keith Barrows

Reputation: 25308

Turns out my site was not accepting the DELETE verb. Removed WebDAVModule and it started working. Now, on to the next problem with the AuthToken.

<system.webServer>
<modules>
<remove name="WebDAVModule" />
...

Upvotes: 0

TheWhiteRabbit
TheWhiteRabbit

Reputation: 15758

check URL Template of your Action also check the method prototype which needs to be String

Upvotes: 0

Esteban Elverdin
Esteban Elverdin

Reputation: 3582

Try this, is slightly different, I guess you are using areas, just replace YourController and 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."); }
});

Upvotes: 1

Related Questions