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.
- Web API with AJAX: Understand POST request in Web API
- Web API with AJAX: Understand GET request in Web API
- Web API with AJAX: Make PUT Request in RESTful Web API Service
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.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="APICall.aspx.cs" Inherits="WebApplication1.APICall" %>
<head runat="server">
<script src="jquery-1.7.1.js" type="text/javascript"></script>
<script>
$(document).ready(function () {
$("#Save").click(function () {
var person = new Object();
person.name = $('#name').val();
person.surname = $('#surname').val();
$.ajax({
url: 'http://localhost:3413/api/person',
type: 'DELETE',
dataType: 'json',
data:person,
success: function (data, textStatus, xhr) {
console.log(data);
},
error: function (xhr, textStatus, errorThrown) {
console.log('Error in Operation');
}
});
});
});
</script>
</head>
<body>
<form name="form1" id="form1">
Name:- <input type="text" name="name" id="name" />
Surname:-<input type="text" name="surname" id="surname" />
<input type="button" id="Save" value="Save Data" />
</form>
</body>
</html>



Nguyễn HoàngPosted Jan 23, 2019, 7:27 PM
Can you send to me the sourcecode?