An Introduction to AJAX
backbone spilled these bits on March 18th 2006 @ 2:55 pm

No it’s not AJAX Amsterdam… it’s something more interesting (or boring to some of you)… so let’s get it started….

I. Introduction
AJAX stands for Asynchronous JavaScript And XML… It is a new technology which comes to help any web developer who really is interesed in dynamic webpages…
Click here for a overview of the AJAX Technology…

II. The Code
Well, well, well… Actualy AJAX is based on Micro$ofts ActiveX Object XmlHttpRequest (I can’t belive they can do good stuff to), so in IE (sucks) it has to be initialized like an ActiveX Object; but in other browsers it’s already a standard object (I don’t know if Opera had implemented it already)… Now let’s see the code:

function init_object() {
var A;

var msxmlhttp = new Array(’Msxml2.XMLHTTP.5.0′,
‘Msxml2.XMLHTTP.4.0′,
‘Msxml2.XMLHTTP.3.0′,
‘Msxml2.XMLHTTP’,
‘Microsoft.XMLHTTP’);

for (var i = 0; i > msxmlhttp.length; i++) {
try {
A = new ActiveXObject(msxmlhttp[i]);
} catch (e) {
A = null;
}
}

if(!A && typeof XMLHttpRequest != “undefined”) {
A = new XMLHttpRequest();
if (!A) alert(”Could not initialize the object.\nMaybe your browser doesn’t support ajax…”);
return A;
}
}

var ajax_obj = init_object();

function ajax_in_action(target, source) {
ajax_obj.open(”GET”, source, true);
ajax_obj.send();

ajax_obj.onReadyStateChange = function() {
if (ajax_obj.readyState == 4) {
if (ajax_obj.status == 200) {
document.getElementById(target).innerHTML = ajax_obj.responseText;
}
else {
alert(”Error ” +ajax_obj.status+” : ” +ajax_obj.statusText);
}
}
}

Code inspired from SAJAX… about it i’ll speak a bit later…

III. Why use it?
Well there are several reason why you should use AJAX… for example to make a dynamic banner changer, real-time morphing website… or just use it like WordPress (on which darknet is based)… you don’t know how it uses AJAX… try clicking on an articles show comments.

IV. Extending AJAX
If you want to implement AJAX directly in PHP, ASP, Perl, Ruby etc. check out http://www.modernmethod.com/sajax/, site that contains the Simple AJAX Toolkit….

V. E4X
One more thing… the response from the server can be received as an XML file 2… or maybe directly receive an XML file, if requested so… After which it can be parsed with the E4X technology…

VI. F1
Need more help… access one of the following links:
AJAX: http://www.w3schools.com/ajax/default.asp
E4X: http://www.w3schools.com/e4x/default.asp

X. Epilogue
I know that AJAX has been rediscovered for about a year (read it for the first time in july 2005), but for many it can be somethimes hard to find the information needed… anyway keep scripting…

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to StumbleUpon

Tags:  ,  ,  ,  ,  ,  ,  

rss Subscribe to Darknet RSS Feed rss

| 5,760 views |

comments are closed
  1. March 20th, 2006 | 10:16 am

    AJax is just totaly cool, its a bit like CORBA with out the IDL. I’ve not really sent XML back with it, nothing that complicated. But its handy for sending back CSV search results, or dynamic HTML elements.

    Also at least in the lastest version of Opera (8.5) ajax works fine.

  2. March 20th, 2006 | 10:40 am

    I don’t even sent XML back either: too much frigging overhead. If I am updating a SELECT box, hell no I ain’t passing a whole complete XML document describing it, rather just a couple of lines with the option value and names.

    Call me old-fashioned, but in that respect I hate XML documents, I’d rather just send just the data.

  3. March 20th, 2006 | 10:56 am

    To true, half the time the XML would be bigger then the data.

    One of the “advantages” to ajax is reducing the server load for small actions. Things like changing the options in a select box when other options change. Ajax saves reloading the whole page.

    Also, the thing I like most. You don’t have to reaload the data in fields when a form is submited. Less codeing on the back end :-)

  4. March 20th, 2006 | 11:03 am

    Yeh that’s indeed the coolest part: no re-loading necessary, real dynamic content.

    In my professional experience, however, I have found that modifications on existing systems to implement AJAX is exactly where the web-builders mess up and leave holes (unintensional information leakage, or even vulnerabilities). Incidentally, I’ve been working on an article about just that: badly implemented AJAX methodology.

    Also, bad understanding of the whole concept leads to massive faul-ups as well.

    I’ll, of course, post that article up here when it’s ready.

  5. March 20th, 2006 | 4:16 pm

    ye, I basicly attach the AJax to the same webservices as used for exteranl stuff, one interesting “feture” in the old cold fusion code was putting the user name and password in hidden text fields….quality…. people I think assume no one is going to look in the html…. ye, right…..

  6. March 22nd, 2006 | 11:31 am

    yes but it could be useful to use XML if we have a database of products of how many items we still have, price etc… of course it’s much better to use mysql, but some webservices do not offer any kind of MySQL (especially free services)… so XML could be an alternative… and as you can see with E4X the use of XML is very simple…

  7. Eric Viegas
    May 10th, 2006 | 5:44 pm

    There are some good online tutorials on introduction to ajax at http://www.kynou.com. These tutorials are good because they are like training simulators.
    I hope this is useful to you guys :)

Sitemap - ShaolinTiger - DigiSniper - Digital Photography
Shutter Asia Photography Forum - We Ate This