Understanding jQuery’s AJAX Function

The Jquery logo

Author :  

The Jquery logo

At first, the jQuery AJAX function can be a bit confusing, especially if you have never worked with AJAX or JSON, it can be a little confusing. Let’s take a step-by-step at jQuery’s AJAX function by requesting a JSON file and retrieving data from it. You can follow along by copying and pasting, or writing, the code into JSFiddle. Before we begin, it is important to note that you should have some knowledge of Javascript and the jQuery library in order to understand this tutorial.

The .ajax() function

The first step is to read about the .ajax() function on the jQuery site; however, the documentation can be a little confusing. The jQuery documentation states that the $.get() and .load() functions are easier to use and retrieve information from another page / website.

But let’s work .ajax() for now. Let’s use JSFiddle to test out our code. Erase the console.log(“Hello World”); function.

Let’s get the information from the following url:

The site contains datasets, or lists, that we can pull information from for testing (and fun!). Let’s get a list the languages and print them out.

As you can see the url contains the following:

This is called a URL parameter. Basically, it is telling browser that we want to load the JSON file. Let’s access the file using AJAX. Here’s the JSFiddle and here’s the code:

If you look at the bottom-right hand side, you should see that the console is outputting a lot of objects. This means that the AJAX call worked!

the console log return

Let’s break the .ajax() function into smaller parts.

Instead of using the ‘$’ sign, you can also use ‘jQuery’ to ensure that here are no conflicts with other libraries:

This will ensure that there are no conflicts! Next, we describe the type of request that we will make. In this case, we are going to be grabbing information from another site, so we should use ‘GET’:

The URL that we would like to retrieve data from:

The dataType which can be: json, jsonp, html and many more.

If the GET request has succeeded, then we should use the success function will run:

The function will then grab the data and pass it into a parameter named “json”. Then, we will log the data from the website into the console. That is what we are seeing here:

the console log return

Close the function:

All together, we get:

The function outputs too much information! Let’s just grab one set. Let’s take a look at the JSON file.

The JSON file

The information has objects stored into an array. It might seem a little complicated, but let’s grab the first set, marked with 0.

The first object printed

We did it! Here’s the JSFiddle if you have been following along.

Now let’s take it a step further and grab some individual pieces. Here’s the code


Let’s take a look at this piece:

This is the name (highlighted in red):

The highlighted red box

To get the language family:

What we are doing is acessing the first index of the array, and then grabbing the fam property. The “.” notation signifies that we are accessing a property or a method of an object (in this case, a property or value).

Finally, let’s append the data to an HTML element:



We are just appending, or adding, the information in. You can check out the final JSFiddle.

Feel free to play around with the code as much as you want. You should now understand how to make a GET request and grab information. Later on, let’s take a look at t a look at the $.get() function.