Create the Namespace in JavaScript-
You can see the below example in Detail-
//AJAX Synchronous and Asynchronous
Requests.
//#REGION NAMESPACE
var
demo = demo
|| {};
//#ENDREGION
demo.ajax
= demo.ajax
|| (function () {
var getBaseURL
= function () {
var
currentBaseURL = location.protocol
+ "//" + location.hostname
+
(location.port
&& ":"
+ location.port)
+ "/";
return
currentBaseURL;
};
var baseURL
= getBaseURL();
var request_token;
var ajaxAsyncCall
= function (requestURL,
typeGP, inputs,
request_token) {
//Ajax Async CALL
$.ajax({
url:
requestURL,
type:
typeGP,
contentType:
"application/json; charset=utf-8",
data:
inputs,
beforeSend:
function (xhr)
{
xhr.setRequestHeader("Request_Token",
request_token)
},
async:
true,
cache:
false,
success:
function (data)
{
if
(data !== undefined
&& data
!== null) {
if
(data.Code
== "OK") {
alert("Success");
return
false;
}
else
if (data.Code
== "ERROR")
{
alert('Error
- ' + data.Message);
return
false;
}
}
}
});
};
var ajaxSyncCall
= function (requestURL,
typeGP, inputs,
request_token) {
//Ajax Sync CALL
$.ajax({
url:
requestURL,
type:
true,
contentType:
"application/json; charset=utf-8",
data:
inputs,
beforeSend:
function (xhr)
{
xhr.setRequestHeader("Request_Token",
request_token)
},
async:
false,
cache:
false,
success:
function (data)
{
if
(data !== undefined
&& data
!== null) {
if
(data.Code
== "OK") {
alert("Success");
return
false;
}
else
if (data.Code
== "ERROR")
{
alert('Error
- ' + data.Message);
return
false;
}
}
}
});
};
return {
baseURL:baseURL,
ajaxAsyncCall:
ajaxAsyncCall,
ajaxSyncCall:
ajaxSyncCall
}
})();
Use of CREATED Synchronous and Asynchronous common methods
in other JavaScript file.
//Use of common Synchronous and
Asynchronous methods.
//GET CUSTOMERS LIST WITH SYNC CALL.
demo.ajax.ajaxSyncCall(demo.ajax.baseURL
+ "API/Users/GetUsersRequest",
'GET', null,
function (data)
{
if (data
!= undefined) {
successGetCustomers(data);
}
}, null);
//GET CUSTOMERS LIST WITH ASYNC CALL.
demo.ajax.ajaxAsyncCall(demo.ajax.baseURL
+ "API/Users/GetUsersRequest",
'GET', null,
function (data)
{
if (data
!= undefined) {
successGetCustomers(data);
}
}, null);
//Success Method
var
successGetCustomers
= function (data)
{
if (data)
{
console.log(data);
//TODO: As PER YOU!
}
};
I hope you are enjoying with this post!
Please share with you friends!! Thank you!!!