Before diving into the core topic let’s have an overview about JQuery and Ajax. What is it?
What is JQuery?
Well, JQuery is a framework (tools) for writing JavaScript, Written as "write less, do more", jQuery is to make easier to use JavaScript.
What is JavaScript?
JavaScript is an object-oriented computer programming (Scripting) language commonly used to create interactive effects within web browsers.
Let’s have a sample example:
We have a submit button in our JQuery AJAX MVC application. Let’s try to show a message when it is clicked.
Here is our button with defined id="btnSubmit":
- <input type="submit" value="Create" class="btn btn-default" id="btnSubmit" />
Now we need to write some script to do that, here is some JavaScript code:
- var myBtn = document.getElementById('btnSubmit');
- myBtn.addEventListener('click', function(e) {
- e.preventDefault(); //This prevents the default action
- alert('Hello'); //Show the alert
- });
By this code the result will show “Hello”:
Now if we can get the same result by using jQuery instead of it. Let’s have a look:
- $('#btnSubmit').click(function(event) {
- event.preventDefault(); //This prevents the default action
- alert("Hello"); //Show the alert
- });
Note: Here use of 'e' is just a short for event which raised the event. We can pass any variable name. Notice that the ‘e’ is changed to name ‘event’ in JQuery part.
This piece of code is also producing the same result “Hello”. This is why jQuery is known as "write less, do more".
Finally the script:
- <script type="text/javascript">
- $(document).ready(function() {
- $(function() {
- /*var myBtn = document.getElementById('btnSubmit');
- myBtn.addEventListener('click', function(e) {
- e.preventDefault();
- alert('Hello');
- });*/
- $('#btnSubmit').click(function(event) {
- event.preventDefault();
- alert("Hello");
- });
- });
- });
- </script>
Let's focus on Ajax:
AJAX stands for “Asynchronous JavaScript and XML”. AJAX is about exchanging data with a server, without reloading the whole page. It is a technique for creating fast and dynamic web pages.
In .NET, we can call server side code using two ways:
- ASP .NET AJAX
- jQuery AJAX
In this article we will focus on JQuery Ajax.
$.ajax () Method:
JQuery’s core method for creating Ajax requests. Here are some jQuery AJAX methods:
- $.ajax() Performs an async AJAX request.
- $.get() Loads data from a server using an AJAX HTTP GET request.
- $.post() Loads data from a server using an AJAX HTTP POST request.
To know more click.
$.ajax () Method Configuration option:
Options that we use:
- async:
- type:
- url:
- data:
- datatype:
- success:
- error:
Let’s have details overview:
async
Set to false if the request should be sent synchronously. Defaults to true.
Note that if you set this option to false, your request will block execution of other code until the response is received.
Example:
- async: false,
type
This is type of HTTP Request and accepts a valid HTTP verb.
The type of the request, "POST" or "GET" defaults to "GET". Other request types, such as "PUT" and "DELETE" can be used, but they may not be supported by all the web browsers.
Example:
- type: "POST",
url
The URL for the request.
Example:
- url: "/Home/JqAJAX",
data
The data to be sent to the server. This can either be an object or a query string.
Example:
- data: JSON.stringify(model_data),
dataType
The type of data you expect back from the server. By default, jQuery will look at the MIME type of the response if no dataType is specified.
Accepted values are text, xml, json, script, html jsonp.
Example:
- dataType: "json",
contentType
This is the content type of the request you are making. The default is 'application/x-www-form-urlencoded'.
Example:
- contentType: 'application/json; charset=utf-8',
success
A callback function to run if the request succeeds. The function receives the response data (converted to a JavaScript object if the DataType was JSON), as well as the text status of the request and the raw request object.
- Success: function (result) {
- $('#result').html(result);
- }
error
A callback function to run if the request results in an error. The function receives the raw request object and the text status of the request.
- error: function (result) {
- alert('Error occured!!');
- },
Let’s Post Values using JQuey,Ajax:
We often use the jQuery Ajax method in ASP.NET Razor Web Pages. Here is a sample code:

Balasubramanian RPosted Aug 13, 2022, 12:07 PM
Good presentation. It is basics, but meaning of each options is understood after working in mvc with ajax.
Jamal RshadPosted Mar 19, 2021, 8:05 PM
Excellent!!
Hamid KhanPosted Feb 28, 2021, 8:02 AM
Very nice article
Prashanth VunnamPosted Aug 26, 2020, 8:13 AM
Great Thanks for posting
Tarun GargPosted Jul 26, 2018, 12:29 AM
Any one can tell me. Why you Use JsonRequestBehavior.AllowGet this.
imran osmanzaiPosted Jan 30, 2018, 2:37 AM
Thanks a lot sir from your posting log life
TaraPosted Sep 29, 2017, 5:42 AM
Very nice and clearly understandable. Thanks for posting.
waqar naeemPosted Aug 21, 2017, 1:47 PM
Very nice article, you said about MIME types, can you please tell me which type of MIME types. see the link https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Complete_list_of_MIME_types
Hemanth KumarPosted Jul 17, 2017, 3:07 AM
Doubt : How is the controller action method linked to the ajax method in the view, i didn't get that part.
Shamim UddinPosted Oct 2, 2016, 1:25 AM
Nice
Santhakumar MunuswamyPosted Oct 18, 2015, 4:47 AM
Good one
Shridhar SharmaPosted Oct 8, 2015, 12:44 PM
nice share
Abhishek KumarPosted Oct 8, 2015, 5:26 AM
good one....
Nilesh JadavPosted Oct 8, 2015, 2:22 AM
Great work