0

I have a web service created by Asp API, and i am trying to consume it by javascript ajax caller .. it works fine with GET & POST .. but when i tried to call DELETE function it returns message [The requested resource does not support http method 'DELETE'.] and this is my code

Server code (API C#)

[HttpDelete]
        public bool Delete(int id)
        {
            try
            {
                var model = db.PostsLikes.First(f => f.PostLikeID == id);
                db.PostsLikes.Remove(model);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }

Client code (Javascript)

function (postLikeid) {
        var result = $.ajax({
            url: "/api/PostsLikes/",
            type: "DELETE",
            async: false,
            data: postLikeid ,
            contentType:"application/json"
        }).responseText;
        return result;
    }
Amir Imam
  • 871
  • 1
  • 12
  • 23
  • 1
    Possible duplicate of [PUT and Delete not working with ASP.NET WebAPI and Database on Windows Azure](http://stackoverflow.com/questions/25835022/put-and-delete-not-working-with-asp-net-webapi-and-database-on-windows-azure) – Heretic Monkey Jan 27 '17 at 16:49
  • Show route config – Nkosi Jan 27 '17 at 18:11

3 Answers3

2

Problem is your IIS configuration is not accepting DELETE verbs. In the Handler Mappings section of IIS you can add the Delete verb.

NibblyPig
  • 51,118
  • 72
  • 200
  • 356
1

Add it in delete method.

[HttpDelete]
[Route("api/PostsLikes/{id}")]
Balaji Marimuthu
  • 1,940
  • 13
  • 13
0
function DeleteFruitRecord(FruitID) {
var del = confirm("Are you sure you want to delete this recored?");
if (del) {
    $.ajax({
        type: "DELETE",
        url: "api/FruitRec/DeleteFruit" + FruitID,
        contentType: "json",
        dataType: "json",
        success: function (data) {
            alert("Successsfully deleted…. " + FruitID);
            GelAllEmployees();
        },
        error: function (error) {
            alert(error.responseText);
        }
    });
}
rahul_k
  • 1
  • 1