Welcome!

Machine Learning Authors: Pat Romanski, Liz McMillan, Yeshim Deniz, Elizabeth White, Zakia Bouachraoui

Related Topics: Machine Learning , Microsoft Cloud

Machine Learning : Article

AJAX-Driven Websites: Under The Hood

Asynchronous JavaScript, XML, and more!

Recently, a number of Web sites have begun to raise some eyebrows within the developer community. What's unique about these sites is that they behave more like a desktop application than a Web application. As you interact with them, they quickly display an endless amount of information to your browser without reloading the page.

At the Google Maps site for example (http://maps.google.com/), you can click on the map, zoom in, zoom out, and move around as much as you like. Your browser continues to be fed with data from the server, yet your browser doesn't have to refresh. They're not using applets, or anything like Flash, so how are they doing it?

Introducing Asynchronous JavaScript + XML, also known as Ajax. To properly describe what Ajax is, it's easiest to contrast it with what it's not. For most Web sites, interaction with a Web server is simplex communication - like talking to your buddy on a walkie-talkie. You speak while he receives, and vice versa, but never at the same time. For a Web user, when he or she fills out an online form and then clicks the submit button, the entire page is posted to the Web server and the user must wait for the server to receive the request. When the server finishes processing the request, it sends the processed content back. Only then does the user's page finally refresh (see Figure 1). Ajax is an attempt to alleviate this choppy sequence of events. When the user is at an Ajax Web site the browser can call the Web server asynchronously, behind the scenes - without posting the entire page.

Nuts & Bolts
Currently there's no SDK for Ajax. It's not something you can download. It is actually a conglomeration of several technologies that may or may not even use XML, despite the fact that XML is represented in the Ajax name. Taking a look underneath the hood, we see a mixture of technologies being used. JavaScript, the DOM, XMLHttp, and XML are the key players. Keep in mind though, there are neither standards nor strict definitions for this methodology. What you find in one implementation may differ from another. However, one thing that is common to Ajax implementations is JavaScript.

As the user interacts with the browser, the JavaScript code will handle various events, such as the keystroke or click events, and deal with them accordingly. JavaScript uses the XMLHttpRequest object as the liaison between the browser and the remote server. Microsoft was first to implement the XMLHttpRequest object in Internet Explorer 5 (since then, the other major browsers have added support for this object as well).

The cool thing about the XMLHttp-Request object is that it can talk with the Web server whilst running in the background, asynchronously, without having to reload the page. When the Web server receives a request from a browser, it does its processing and then returns processed XML data back to the browser. The JavaScript engine will receive this processed XML, and use the DOM to manipulate the page elements accordingly. For example, in an Ajax-driven page, such as the Google Suggest site (www.google.com/webhp?complete=1&hl=en), as you type in the search field each letter is sent to the server asynchronously. When typing, words quickly appear below the text. Behind the scenes, it's making several calls to the server for each keystroke. The user isn't hassled by this, because their interaction isn't being interrupted. Only a portion of the page is being refreshed. This can all be done efficiently, because only a fraction of the page data is sent over the wire, rather than the entire page.

An Ajax Example
Let's take a look at a simple Ajax example. It consists of only two pages, HTMLStartPage.htm, and HandleAjaxRequests.aspx. HTMLStartPage.htm contains two text boxes. This demonstrates how characters typed by the user can be sent to the HandleAjaxRequests.aspx page one by one, processed, and then echoed back to HTMLStartPage.htm's second text box. Figure 2 is a visual representation of this example.

The meat of Ajax lies in the JavaScript. Looking at the code, we see that the text box, txtStart, calls the SendValue(val) function for any onKeyUp events within txtStart.

When a user types a character and the SendValue(Val) is called, the HTTPRequest object is initialized first. At this point we determine if the current browser is IE, or some-thing else like Mozilla or Netscape, so we can create the objRequest (HTTPRequest) correctly. Next, to handle things on the server side, we load the "url" variable with the location of the aspx page that will do our processing.

Next, let's look at the three objRequest lines. First, the objRequest.onreadystatechange is called. The onreadystatechange property helps us set up a callback function. This callback will be called only when the readyState property changes; that is, when we get data back from the Web server. The callback function will handle it at that time.

The objRequest.open requires three parameters: a GET or POST, a string for the "url" we defined earlier, and a Boolean value that defines whether this call is asynchronous or not. Note that if this Boolean value is set to true, as it is here, a callback function is required.

The objRequest.send(null) line actually calls the aspx page defined in the "url" variable. However, before we go on to the aspx page, notice the callback function (Process()). Remember, this is the function that will be called (back) after the Web page code has processed our request; it's our reentry point. Here, we simply take the values returned from our aspx page, and put them into the second text box (txtEchoOutPut).

For our aspx page, I made things as simple as possible. It receives the keystroke as a querystring value and I add some text to the string ("You typed:") to prove that we hit the server, and then we echo back the same text. After this aspx page is hit, the callback function (Process()) is fired within the JavaScript, as described earlier.

From the user's perspective, this is all done very quickly. When a user types in the first text box, their letters appear within the second text box immediately. The typed text is actually making a round trip to the server and back.

Ajax Is Not New
It should be noted that Ajax is not new. The methodology has been around for years. Web sites like Google are now proving Ajax's usefulness, stability, and the ability to make the Web closely resemble that of a desktop application: the holy grail of Web development. And what's special about Ajax is that it can do all of this using proven, existing technology. In other words, any standard browser (that can handle JavaScript and the DOM) will work. You don't need to install something separate, like a plug-in.

Here at Magenic, we're taking a look at how this methodology can benefit our clients. Ajax is not something that will replace every Web site, as we know it, but it has a place and it's a skill we want to have in our repertoire.

More Stories By Tommy Newcomb

Tommy Newcomb works for Magenic as an IT consultant in the Chicago area.
His main focus is developing Web application and E-commerce work using Microsoft technologies. He lives with his wife, Emily, and baby daughter, Jaqueline, in the Chicago suburbs.

Comments (7)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


CloudEXPO Stories
Isomorphic Software is the global leader in high-end, web-based business applications. We develop, market, and support the SmartClient & Smart GWT HTML5/Ajax platform, combining the productivity and performance of traditional desktop software with the simplicity and reach of the open web. With staff in 10 timezones, Isomorphic provides a global network of services related to our technology, with offerings ranging from turnkey application development to SLA-backed enterprise support. Leading global enterprises use Isomorphic technology to reduce costs and improve productivity, developing & deploying sophisticated business applications with unprecedented ease and simplicity.
While a hybrid cloud can ease that transition, designing and deploy that hybrid cloud still offers challenges for organizations concerned about lack of available cloud skillsets within their organization. Managed service providers offer a unique opportunity to fill those gaps and get organizations of all sizes on a hybrid cloud that meets their comfort level, while delivering enhanced benefits for cost, efficiency, agility, mobility, and elasticity.
DevOps has long focused on reinventing the SDLC (e.g. with CI/CD, ARA, pipeline automation etc.), while reinvention of IT Ops has lagged. However, new approaches like Site Reliability Engineering, Observability, Containerization, Operations Analytics, and ML/AI are driving a resurgence of IT Ops. In this session our expert panel will focus on how these new ideas are [putting the Ops back in DevOps orbringing modern IT Ops to DevOps].
Darktrace is the world's leading AI company for cyber security. Created by mathematicians from the University of Cambridge, Darktrace's Enterprise Immune System is the first non-consumer application of machine learning to work at scale, across all network types, from physical, virtualized, and cloud, through to IoT and industrial control systems. Installed as a self-configuring cyber defense platform, Darktrace continuously learns what is ‘normal' for all devices and users, updating its understanding as the environment changes.
Enterprises are striving to become digital businesses for differentiated innovation and customer-centricity. Traditionally, they focused on digitizing processes and paper workflow. To be a disruptor and compete against new players, they need to gain insight into business data and innovate at scale. Cloud and cognitive technologies can help them leverage hidden data in SAP/ERP systems to fuel their businesses to accelerate digital transformation success.