Introduction
Ajax (Asynchronous JavaScript + XML) is a new approach now a days that is very popular in web development.
Ajax has taken our classic web development to a different level. It is a new approach to create fast and dynamic web pages. It totally removed the reloading part from the classical web development.
Background
There are many Ajax methods provided by jQuery library to accomplish Ajax calls as listed below:
get
post
getScript
getJSON
ajax
All these Ajax methods can be used as per requirements you are having but, if you need extensive configurability and want to handle errors by yourself then '.ajax()
' method is the best.
To work with the jQuery Ajax method, we need to set the options available for it properly, from which
'async
' is an important setting about which we are going to discuss here as setting its value will have a major impact in getting response properly.
As async is an boolean property, we can set either TRUE
or FALSE
value for it. Here, I will explain various scenarios on setting each values for the particular property (async
) and to help you in understanding its importance and usability by situation.
Using the Code
Here, I am explaining simple examples of how and when to make your choice in between asynchronous or synchronous ajax calls using jQuery.
Both methods independent of each other:
There will be no impact on result if both are independent, whether it is asynchronous or synchronous Ajax calls:
$(function () {
GetData1(false);
GetData2(false);
?
GetData1(true);
GetData2(true);
});
First Method
function GetData1(isAsync) {
$.ajax({
type: "POST",
url: "../WebMethods/WebService.asmx/GetData1",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: isAsync,
success: function (response) {
var result = JSON.parse(response.d);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
}
Second Method
function GetData2(isAsync) {
$.ajax({
type: "POST",
url: "../WebMethods/WebService.asmx/GetData2",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: isAsync,
success: function (response) {
var result = JSON.parse(response.d);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
}
Second method dependent on first one:
But when the second method is dependent on result supplied from the first method, it will definitely have an impact:
$(function(){
var result = GetData3(false);
GetData4(false, result);
var result = GetData3(true);
GetData4(true, result);
});
First Method
function GetData3(isAsync) {
var result = null;
$.ajax({
type: "POST",
url: "../WebMethods/WebService.asmx/GetData3",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: isAsync,
success: function (response) {
result = JSON.parse(response.d);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
return result;
}
Second Method
Takes an extra argument which is the result of the first method.
function GetData4(isAsync, result) {
$.ajax({
type: "POST",
url: "../WebMethods/WebService.asmx/GetData4",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: isAsync,
success: function (response) {
if (result.id == 1) {
var result = JSON.parse(response.d);
}
else alert('No operation required..');
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
}
Points of Interest
This will definitely be of help to developers who started working on jQuery Ajax method. There are many scenarios and requirements, we need to understand the fundamentals so that we can make the right choice at the right place.