Machine Learning Authors: Liz McMillan, Janakiram MSV, Roger Strukhoff, Yeshim Deniz, Pat Romanski

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
Cloud-Native thinking and Serverless Computing are now the norm in financial services, manufacturing, telco, healthcare, transportation, energy, media, entertainment, retail and other consumer industries, as well as the public sector. The widespread success of cloud computing is driving the DevOps revolution in enterprise IT. Now as never before, development teams must communicate and collaborate in a dynamic, 24/7/365 environment. There is no time to wait for long development cycles that produce software that is obsolete at launch. DevOps may be disruptive, but it is essential. DevOpsSUMMIT at CloudEXPO expands the DevOps community, enable a wide sharing of knowledge, and educate delegates and technology providers alike.
In a recent survey, Sumo Logic surveyed 1,500 customers who employ cloud services such as Amazon Web Services (AWS), Microsoft Azure, and Google Cloud Platform (GCP). According to the survey, a quarter of the respondents have already deployed Docker containers and nearly as many (23 percent) are employing the AWS Lambda serverless computing framework. It's clear: serverless is here to stay. The adoption does come with some needed changes, within both application development and operations. That means serverless is also changing the way we leverage public clouds. Truth-be-told, many enterprise IT shops were so happy to get out of the management of physical servers within a data center that many limitations of the existing public IaaS clouds were forgiven. However, now that we've lived a few years with public IaaS clouds, developers and CloudOps pros are giving a huge thumbs down to the...
Kubernetes is an open source system for automating deployment, scaling, and management of containerized applications. Kubernetes was originally built by Google, leveraging years of experience with managing container workloads, and is now a Cloud Native Compute Foundation (CNCF) project. Kubernetes has been widely adopted by the community, supported on all major public and private cloud providers, and is gaining rapid adoption in enterprises. However, Kubernetes may seem intimidating and complex to learn. This is because Kubernetes is more of a toolset than a ready solution. Hence it’s essential to know when and how to apply the appropriate Kubernetes constructs.
To enable their developers, ensure SLAs and increase IT efficiency, Enterprise IT is moving towards a unified, centralized approach for managing their hybrid infrastructure. As if the journey to the cloud - private and public - was not difficult enough, the need to support modern technologies such as Containers and Serverless applications further complicates matters. This talk covers key patterns and lessons learned from large organizations for architecting your hybrid cloud in a way that: Supports self-service, "public cloud" experience for your developers that's consistent across any infrastructure. Gives Ops peace of mind with automated management of DR, scaling, provisioning, deployments, etc.
xMatters helps enterprises prevent, manage and resolve IT incidents. xMatters industry-leading Service Availability platform prevents IT issues from becoming big business problems. Large enterprises, small workgroups, and innovative DevOps teams rely on its proactive issue resolution service to maintain operational visibility and control in today's highly-fragmented IT environment. xMatters provides toolchain integrations to hundreds of IT management, security and DevOps tools. xMatters is the primary Service Availability platform trusted by leading global companies and innovative challengers including BMC Software, Credit Suisse, Danske Bank, DXC technology, Experian, Intuit, NVIDIA, Sony Network Interactive, ViaSat and Vodafone. xMatters is headquartered in San Ramon, California and has offices worldwide.