Web API With AJAX: Understand DELETE Verb in Restful Web API

This article provides an example showing the DELETE HTTP verb and how to generate a delete request using the jQuery ajax() function.

This is the "Web API with AJAX" article series. Here we are learning to call and consume a Web API action using jQuery AJAX method. In our previous articles we have learned to consume a Web API method using POST , GET and PUT verb. You can visit them by clicking the following URL.
This article provides an example showing the DELETE HTTP verb and how to generate a delete request using the jQuery ajax() function. Previous articles in this series have explained much about API and RESTful service and it's very simple to understand that the Delete operation in CRUD is mapped with the DELETE HTTP verb n terms of RESTful service. So, let's learn it with the example.
 
Create client code to make DELETE request
 
Here we will make a DELETE request using the jQuery ajax() function of the Web API. So, here is the code implementation. We are sending a complex object to perform the operation.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="APICall.aspx.cs" Inherits="WebApplication1.APICall" %>  
  2. <head runat="server">  
  3.     <script src="jquery-1.7.1.js" type="text/javascript"></script>  
  4.      <script>  
  5.          $(document).ready(function () {  
  6.              $("#Save").click(function () {  
  7.                  var person = new Object();  
  8.                  person.name = $('#name').val();  
  9.                  person.surname = $('#surname').val();  
  10.                  $.ajax({  
  11.                      url: 'http://localhost:3413/api/person',  
  12.                      type: 'DELETE',  
  13.                      dataType: 'json',  
  14.                      data:person,  
  15.                      success: function (data, textStatus, xhr) {  
  16.                          console.log(data);  
  17.                      },  
  18.                      error: function (xhr, textStatus, errorThrown) {  
  19.                          console.log('Error in Operation');  
  20.                      }  
  21.                  });  
  22.              });  
  23.          });  
  24.     </script>  
  25. </head>  
  26. <body>  
  27.     <form name="form1" id="form1">  
  28.         Name:- <input type="text"  name="name" id="name" />  
  29.         Surname:-<input type="text"  name="surname" id="surname" />  
  30.         <input type="button" id="Save" value="Save Data" />  
  31.     </form>  
  32. </body>  
  33. </html>  
So, now we need to implement the API functionality to serve the DELETE request from the client and for that we have implemented a Delete() action attributed as "HttpDelete".
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Net.Http;  
  6. using System.Web.Http;  
  7. namespace WebApplication1.WebAPI  
  8. {  
  9.     public class person  
  10.     {  
  11.         public string name { getset; }  
  12.         public string surname { getset; }  
  13.     }  
  14.     public class personController : ApiController  
  15.     {  
  16.         [HttpDelete]  
  17.         public string Delete(person data)  
  18.         {  
  19.             return data.name + data.surname;  
  20.         }  
  21.     }  
  22. }  
We are halting the application to check whether data is coming or not. Yes, data is coming for the Delete() action.
 
 
The success callback function is logging this return value to the console. Here is the output.
 
 
DELETE operation by sending parameters in FormBody and FormUri
 
This example is very similar to the above operation and here we are sending one value through the URI and one value through the form body. Here is the implementation of the client code.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="APICall.aspx.cs" Inherits="WebApplication1.APICall" %>  
  2. <head runat="server">  
  3.     <title></title>  
  4.     <script src="jquery-1.7.1.js" type="text/javascript"></script>  
  5.      <script>  
  6.          $(document).ready(function () {  
  7.              $("#Save").click(function () {  
  8.                  $.ajax({  
  9.                      url: 'http://localhost:3413/api/person?ID=100',  
  10.                      type: 'DELETE',  
  11.                      dataType: 'json',  
  12.                      data: {"":"Sourav Kayal"},  
  13.                      success: function (data, textStatus, xhr) {  
  14.                          console.log(data);  
  15.                      },  
  16.                      error: function (xhr, textStatus, errorThrown) {  
  17.                          console.log('Error in Operation');  
  18.                      }  
  19.                  });  
  20.              });  
  21.          });  
  22.     </script>  
  23. </head>  
  24. <body>  
  25.     <form name="form1" id="form1">  
  26.         <input type="button" id="Save" value="Save Data" />  
  27.     </form>  
  28. </body>  
  29. </html>  
The following is the implementation of Web API to accept a parameter from the URI and request:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Net.Http;  
  6. using System.Web.Http;  
  7. namespace WebApplication1.WebAPI  
  8. {  
  9.    public class personController : ApiController  
  10.     {  
  11.         [HttpDelete]  
  12.         public string Delete([FromUri] int ID, [FromBody] String name)  
  13.         {  
  14.             return "Delete Operation" + "ID:- " + ID + "Name:- " + name;  
  15.         }  
  16.     }  
  17. }  
Here is sample output of the example above.