Making your first AJAX call
Now that we have an active JSON data feed, it is about time to make our first AJAX call. We will look at two approaches of making an AJAX call; these approaches come from different periods in time. The first approach will use basic JavaScript so that we understand what happens behind the scenes when an AJAX call is made. Once we understand the concept of AJAX, we will use a popular JavaScript library to make the same AJAX call but with simpler code. Let's take a look at our first approach using basic JavaScript:
We will begin with our basic index.html
file that loads an external JavaScript file. This JavaScript file performs the AJAX call to fetch the students
JSON feed.
Let us take a look at index.js
:
This is the original way in which an AJAX call is made to a live web server; let's break this script into pieces and investigate it piece by piece:
In the preceding snippet we are creating an instance of the XMLHttpRequest
object. The XMLHttpRequest
object lets us make...