This is "Wev-API with AJAX" article series. In our previous two articles we have understood to make POST and GET request to a Web API restful service. If you are new in this series then feel free to visit them.
- Web API with AJAX: Understand POST request in Web API
- Web API with AJAX: Understand GET request in Web API
So, basically we are trying to map CRUD operations with RESTful services and we have learned that the POST method is mapped with a Create operation, in other words when we want to send data to a RESTful service. The GET verb is mapped with a Read operation, in other words we want to get data from a Web server (read RESTful Web API). This article explains the Update operation. The Update operation is mapped with the PUT verb. So when we want to make an update operation then we should make a PUT request. Now, the question is.
Please, try to understand the question! (Ha..Ha..) To do an update operation we need to send data, I mean fresh data that will replace the old. The update operation will be done in our DB layer and the BL layer (let's not debate it) depending on logic. Now the duty of the ajax() function is to send to the Web API and we know that the POST verb is enough to send data to the server. Then why PUT?
I hope you understood the question. Before writing the data for this article I did not find the proper answer. But if we observe the default controller template then we will find that generally, a PUT action takes two parameters as in the following.
// PUT api/<controller>/5
public void Put(int id, [FromBody]string value)
{
}
But the default format for the POST action is as in the following.
public void Post([FromBody]string value)
{
}
So, Microsoft's MVC developer expects people (if you think you are a programmer then replace people with programmer) will send two values at the time of the update operation; and yes, the first value is obviously the ID or any Key and if you think a little then you may use Post() instead of Put(). Maybe they have given to map with CRUD operation.
NB: Expecting .NETians to comment on this topic.
Ok. However let's come to practical implementation. In this example we will make a PUT request to the Web API using an ajax() function. Have a look at the following code.
Implement client-side code to make PUT request
This is the implementation of the JavaScript ajax() function.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="APICall.aspx.cs" Inherits="WebApplication1.APICall" %>
<head id="Head1" runat="server">
<script src="jquery-1.7.1.js" type="text/javascript"></script>
<script>
$(document).ready(function () {
var person = new Object();
person.name = "Sourav";
person.surname = "Kayal";
$("#Save").click(function () {
$.ajax({
url: 'http://localhost:3413/api/person',
type: 'PUT',
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>
<input type="button" id="Save" value="Save Data" />
</body>
</html>



Gowtham RajamanickamPosted Jan 6, 2016, 1:21 AM
good article..