Welcome!

Java IoT Authors: Yeshim Deniz, Zakia Bouachraoui, Elizabeth White, Pat Romanski, Liz McMillan

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.


IoT & Smart Cities Stories
Andrew Keys is Co-Founder of ConsenSys Enterprise. He comes to ConsenSys Enterprise with capital markets, technology and entrepreneurial experience. Previously, he worked for UBS investment bank in equities analysis. Later, he was responsible for the creation and distribution of life settlement products to hedge funds and investment banks. After, he co-founded a revenue cycle management company where he learned about Bitcoin and eventually Ethereal. Andrew's role at ConsenSys Enterprise is a mul...
In his general session at 19th Cloud Expo, Manish Dixit, VP of Product and Engineering at Dice, discussed how Dice leverages data insights and tools to help both tech professionals and recruiters better understand how skills relate to each other and which skills are in high demand using interactive visualizations and salary indicator tools to maximize earning potential. Manish Dixit is VP of Product and Engineering at Dice. As the leader of the Product, Engineering and Data Sciences team at D...
Dynatrace is an application performance management software company with products for the information technology departments and digital business owners of medium and large businesses. Building the Future of Monitoring with Artificial Intelligence. Today we can collect lots and lots of performance data. We build beautiful dashboards and even have fancy query languages to access and transform the data. Still performance data is a secret language only a couple of people understand. The more busine...
Nicolas Fierro is CEO of MIMIR Blockchain Solutions. He is a programmer, technologist, and operations dev who has worked with Ethereum and blockchain since 2014. His knowledge in blockchain dates to when he performed dev ops services to the Ethereum Foundation as one the privileged few developers to work with the original core team in Switzerland.
René Bostic is the Technical VP of the IBM Cloud Unit in North America. Enjoying her career with IBM during the modern millennial technological era, she is an expert in cloud computing, DevOps and emerging cloud technologies such as Blockchain. Her strengths and core competencies include a proven record of accomplishments in consensus building at all levels to assess, plan, and implement enterprise and cloud computing solutions. René is a member of the Society of Women Engineers (SWE) and a m...
Whenever a new technology hits the high points of hype, everyone starts talking about it like it will solve all their business problems. Blockchain is one of those technologies. According to Gartner's latest report on the hype cycle of emerging technologies, blockchain has just passed the peak of their hype cycle curve. If you read the news articles about it, one would think it has taken over the technology world. No disruptive technology is without its challenges and potential impediments t...
If a machine can invent, does this mean the end of the patent system as we know it? The patent system, both in the US and Europe, allows companies to protect their inventions and helps foster innovation. However, Artificial Intelligence (AI) could be set to disrupt the patent system as we know it. This talk will examine how AI may change the patent landscape in the years to come. Furthermore, ways in which companies can best protect their AI related inventions will be examined from both a US and...
Bill Schmarzo, Tech Chair of "Big Data | Analytics" of upcoming CloudEXPO | DXWorldEXPO New York (November 12-13, 2018, New York City) today announced the outline and schedule of the track. "The track has been designed in experience/degree order," said Schmarzo. "So, that folks who attend the entire track can leave the conference with some of the skills necessary to get their work done when they get back to their offices. It actually ties back to some work that I'm doing at the University of San...
When talking IoT we often focus on the devices, the sensors, the hardware itself. The new smart appliances, the new smart or self-driving cars (which are amalgamations of many ‘things'). When we are looking at the world of IoT, we should take a step back, look at the big picture. What value are these devices providing. IoT is not about the devices, its about the data consumed and generated. The devices are tools, mechanisms, conduits. This paper discusses the considerations when dealing with the...
Bill Schmarzo, author of "Big Data: Understanding How Data Powers Big Business" and "Big Data MBA: Driving Business Strategies with Data Science," is responsible for setting the strategy and defining the Big Data service offerings and capabilities for EMC Global Services Big Data Practice. As the CTO for the Big Data Practice, he is responsible for working with organizations to help them identify where and how to start their big data journeys. He's written several white papers, is an avid blogge...