Easy Ajax
Anyone that has ever built a large scale web application using ajax has probably incurred many of the same issues. One of the major problems is that ajax calls are asynchronous. This poses an interesting problem with creating and updating elements on the page accurately. Unless you reload that entire section of the page you will soon see that some object ID’s are saving to the wrong places. Another problem is long requests that are no longer valid. Most modern web browsers will only allow 2 ajax requests to be processed simultaneously The remainder of the requests will be queued up and run in turn. This means that if you are still processing an old request, you are losing half your call speed if not all of it. The last problem that I was really interested in dealing with is visualizing the data that was actually being transfered through the requests. You can read more about that here: validator
The AJAX wrapper
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
var ajax = new AJAX();
function AJAX(){
var callID=0;
/***********************************************
* Perform an ajax request using GET
* url: the url to perform the GET request against
* data: parameter should look like this: { mystring: "mystring", mynumber: 0 }
* callback: a function to return the data back to
* return: the callID of the request
*/
this.Get = function(url, data, callback){
return PerformAJAXRequestGet(url, data, callback);
}
/***********************************************
* Perform an ajax request using POST
* url: the url to perform the POST request against
* data: parameter should look like this: { mystring: "mystring", mynumber: 0 }
* callback: a function to return the data back to
* return: the callID of the request
*/
this.Post = function(url, data, callback){
return PerformAJAXRequest(url, data, callback);
}
//Clears a request based on it's callID
this.RemoveRequest = function(id) {
for (var i = 0; i < requestArray.length; i++) {
if (requestArray[i].id == id){
requestArray[i].xhr.abort();
if (RUNVALIDATOR)
validator.AddItem('Aborted' + requestArray[i].id, '', requestArray[i].id, false);
requestArray.splice(i, 1);
}
}
}
//Clear all currently open requests
this.ClearRequestQueue = function () {
for (var i = 0; i < requestArray.length; i++) {
requestArray[i].xhr.abort();
if (RUNVALIDATOR)
validator.AddItem('Aborted' + requestArray[i].id, '', requestArray[i].id, false);
requestArray.splice(i, 1);
}
}
var requestArray = [];
function PerformAJAXRequest(url, data, callback) {
//Uses the Validator. For more info see
if (RUNVALIDATOR)
validator.AddItem(url, data, callID, true);
var xhr = $.ajax({
url: url,
type: "POST",
data: data,
contentType: "application/json;charset=utf-8",
dataType: "json",
headers: { callID: callID }
}).done(function (val) {
if (RUNVALIDATOR)
validator.AddItem('Recieved' + this.headers.callID, val, this.headers.callID, false);
var id = this.headers.callID;
if (val == "Session Error")
window.location = '../../Login.php';
if (callback != null && typeof callback != 'undefined')
callback(val, id);
});
requestArray.push({ xhr: xhr, id: callID });
callID++;
return callID -1;
}
function PerformAJAXRequestGet(url, data, callback) {
if (RUNVALIDATOR)
validator.AddItem(url, data, callID, true);
var xhr = $.ajax({
url: url,
type: "GET",
data: data,
contentType: "application/json;charset=utf-8",
dataType: "json",
headers: { callID: callID }
}).done(function (val) {
if (RUNVALIDATOR)
validator.AddItem('Recieved' + this.headers.callID, val, this.headers.callID, false);
var id = this.headers.callID;
if (val == "Session Error")
window.location = '../../Login.php';
if (callback != null && typeof callback != 'undefined')
callback(val, id);
});
requestArray.push({ xhr: xhr, id: callID });
callID++;
return callID -1;
}
} |
This little library allows us to make both POST and GET calls using ajax while enabling us to track the ID of each request and also stop one or all of the requests from happening. There are 4 functions that you have direct access to.
Get
|
1 2 3 |
var callID = ajax.Get('myAPI.php', { myString: 'my string' }, function(results){
//Do something with the returned data
}); |
Post
|
1 2 3 |
var callID = ajax.Post('myPostAPI.php', { myString: 'my string' }, function(results){
//Do something with the returned data
}); |
RemoveRequest
|
1 2 |
//removes request with ID = 5 if it is currently open
ajax.RemoveRequest(5); |
ClearRequestQueue
|
1 2 |
//Removes all pending requests
ajax.ClearRequestQueue(); |
Conclusion
Using this little helper library, you can resolve some of the rather tricky issues that come up when dealing with complex ajax applications. Plus this will allow you to implement the validator quickly and simply.

















