Introduction
These days, most of the web applications are using AJAX concepts to create better and more responsive applications. AJAX reduces the traffic between client and server and also, makes the response time faster which directly increases the performance of an application.
What is AJAX?
Asynchronous JavaScript and XML (AJAX) is a development technique used to create interactive web applications or rich internet applications. AJAX uses a number of existing technologies together, including XHTML, CSS, JavaScript, Document Object Model, XML, XSLT, and the XMLHttpRequest object.
With AJAX, web applications can retrieve data from the server asynchronously, in the background, without reloading the entire browser page. The use of AJAX has led to an increase in interactive animation on web pages.

Advantages
- Reduces the traffic travels between the client and the server.
- No cross-browser pain.
- Better interactivity and responsiveness.
- With AJAX, several multi-purpose applications and features can be handled using a single web page(SPA).
- APIs are good because they work with the HTTP method and JavaScrtipt.
Disadvantages
- Search engines like Google would not be able to index an AJAX application.
- It is totally built-in JavaScript code. If any user disables JS in the browser, it won't work.
- The server information can not be accessed within AJAX.
- Security is less in AJAX applications as all the files are downloaded at client side.
- The data of all requests is URL-encoded, which increases the size of the request.
For more on how AJAX works, follow here.
Using Code
We will discuss how to transfer the data through AJAX.
- Server To Client
- Client To Server
We use Employee as an entity to transfer the data from Client to Server and vice-versa. Employee contains two fields: ID and Name.
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
}
Server To Client (ASP.NET MVC)
Add the following code in the Controller action. The controller is Employee and the action name is GetEmpList which takes a list of employees as a parameter.
public List<Employee> GetEmpList()
{
var empList = new List<Employee>()
{
new Employee { ID = 1, Name = "Manas" },
new Employee { ID = 2, Name = "Tester" }
};
return empList;
}
Now, we send an AJAX request to the Server to get an Employee list. Firstly, add a jQuery CDN (Content Delivery Network) reference which loads the jQuery library. Second, the code block to get a list of employees from the Server and display it.
Here, this method type is "GET", which gets data in success properties as a response parameter. It loops over the employee list, collects employee data, and binds it as a div.
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
function getEmployees() {
$.ajax({
type: "GET",
url: 'Employee/GetEmpList',
data: {},
contentType: "application/json; charset=utf-8",
dataType: "json",
beforeSend: function(){
Show(); // Show loader icon
},
success: function (response) {
// Looping over employee list and display it
$.each(response, function (index, emp) {
$('#output').append('<p>Id: ' + emp.ID + '</p>' +
'<p>Id: ' + emp.Name + '</p>');
});
},
complete: function(){
Hide(); // Hide loader icon
},
error: function (jqXHR, textStatus, errorThrown) {
alert("HTTP Status: " + jqXHR.status + "; Error Text: " + jqXHR.responseText); // Display error message
}
});
}
</script>
Add the following code in the body tag to display a loader icon when an AJAX request is sent to the Server. It hides the loader icon when AJAX completes (defined in the above AJAX code).
<body>
<div id="div_Loader" style="padding-left: 400px; top: 500px">
<img src="Loading.gif" width="100px" height="100px" alt="loader" />
</div>
<div id="output">
</div>
</body>
Client To Server (ASP.NET MVC)
In the above example, we discussed how to send data from the Server to the Client. Next, let's see how to send data from Client to Server. Here, we will send a list of Employees to the Server to save those in the database.
Add the following code in the Controller's action. The controller is Employee and the action name is SaveEmpList which takes a list of employees as a parameter.
public static bool SaveEmpList(List<Employee> empList)
{
try
{
// Implement your logic to save EmpList in Database
// For example:
// dbContext.Employees.AddRange(empList);
// dbContext.SaveChanges();
}
catch (Exception ex)
{
// Log Error
return false;
}
return true;
}
Here, we will send complex objects as a list of employees to the Server. It creates an array and pushes JSON object to it. Then, it converts the array to JSON string using the JSON.stringify() method to send the data properly.
<script type="text/javascript">
$(function () {
var results = new Array();
var emp1 = { "ID": "12", "Name": "Manas" };
results.push(emp1);
var emp2 = { "ID": "2", "Name": "Tester" };
results.push(emp2);
// Without array you can use like to construct JSON object
// var results = { empList : [{ "ID": "1", "Name": "Manas" },
// { "ID": "2", "Name": "Tester" }] };
var postData = { empList: results };
$.ajax({
url: 'WebForm1.aspx/SaveEmpList',
data: JSON.stringify(postData),
type: 'POST',
contentType: 'application/json',
dataType: 'json',
beforeSend: function(){
Show(); // Show loader icon
},
success: function (result) {
alert(result);
},
complete: function(){
Hide(); // Hide loader icon
},
failure: function (jqXHR, textStatus, errorThrown) {
alert("Status: " + jqXHR.status + "; Error: " + jqXHR.responseText); // Display error message
}
});
});
$(document).ready(function () {
$("#div_Loader").hide();
});
function Show() {
$('#div_Loader').show();
}
function Hide() {
$('#div_Loader').hide();
}
</script>
Above, we discussed how can we implement AJAX in jQuery using $.Ajax. MVC also facilitates two AJAX helpers to implement the same: Ajax.BeginForm, Ajax.ActionLink.
Ajax.BeginForm
There are two types of the BeginForm() extension methods.
Html.BeginForm()
Ajax.BeginForm()
Html.BeginForm() submits data to the Server with full-page postback, which means it re-loads the whole page when a postback occurs. Ajax.BeginForm is used to submit form data to the server without whole page postback, it sends data through AJAX concept.
The Ajax.BeginForm takes the following parameters.
- actionName
- controllerName
- routeValues
- ajaxOptions
- htmlAttributes
ActionName
This parameter is used to define the action name which will be executed.

Saravanakumar SekaranPosted May 10, 2017, 3:13 AM
Wow superb article Manas
Aappasaheb TawalePosted Jan 12, 2017, 11:01 AM
Really, very good article... thanks Manas
Aleena SaviourPosted Nov 28, 2016, 7:22 AM
Good article.....helpful....
Manav PandyaPosted Nov 16, 2016, 10:03 AM
My request is to create video tutorial series and upload it in Youtube if possible Manas Mohapatra
Manav PandyaPosted Nov 16, 2016, 10:02 AM
Nice and simple expaination sir Manas Mohapatra
Madhanmohan DevarajanPosted Nov 16, 2016, 9:37 AM
This is simple and easy to understand, good job Manas!