AJAX

Projected Time

About 4 hours

Prerequisites

Motivation

Front-end code of interactive websites often needs to talk to backend servers to get and present data that the user asks for. Usually, this is done using API calls. AJAX is a way to make asynchronous calls to the server using JavaScript.

Objectives

Participants will be able to:

Materials

Lesson

What is AJAX ?

AJAX stands for Asynchronous JavaScript and XML. It is a technique by which the front-end gets data by making asynchronous calls either to the project’s API in the back-end, or a 3rd-party API. Because AJAX is asynchronous, you must pass in a callback function to handle the received data. Making an AJAX request without any callback function will not have any effect on your application.

What does AJAX do?

Let’s say this website that the end user is visiting is an e-commerce site. The end user is browsing a list of products, sees a product they want to buy, and clicks the “Buy” button. This action (clicking the “Buy” button) triggers the request/response steps listed above, this time for the purpose of putting the product in the shopping cart.

Before AJAX, the request/response cycle could only happen with the web page as a whole. The end user, by clicking the “Buy” button, would cause the backend server to generate a completely new set of HTML and JavaScript, and the browser would reload and render it as an entirely new page.

AJAX, however, enabled the browser to make requests and only re-render parts of a webpage. This was beneficial performance-wise, because smaller strings of HTML, rather than all the HTML for the entire page, was all that was needed to be sent by the server and rendered by the browser. This also made the end user’s browsing experience smoother: interactions with the webpage resulted in changes on screen without being interrupted by a reload.

Full Page Load vs AJAX

Lets say there is an input box and a button. Onclick of button the page shows some data say hello world send by the server. When the button is clicked -

Full load Application Flow

  1. The browser sends a request to a server designated by the URL.
  2. The server does some processing, probably queries a database, then responds to the browser by sending back HTML and JavaScript
  3. The browser renders that HTML and JavaScript into a webpage.

AJAX Flow

  1. The client sends a request to the server.
  2. The server does some processing and returns the corresponding data (usually in JSON format).
  3. When the request is successful, the callback function is executed with the new data. The callback function could do anything you like; for example, it could use JS to add the new data to the HTML page
Difference Point Full Load Page AJAX
Client requests to server waits for response can perform other task simultaneously
Server Response responds a corresponding HTML page responds with data (preferably JSON )
Render a complete new page is rendered only that part of page changes which has requested
Thread Blocked true false
Data Received by the browser in form of HTML JavaScript (callback or Promise)

Now Let us understand this concept in more brief by implementing it practically. Follow the Link, clone the repository and start working.

Remember

It is important to understand the concept of Thread of Execution before we start with AJAX.

Hands-On Practice

Let’s create a simple button. On click of this button the browser will fetch data from the server and that data we will display on DOM using javascript.

  1. HTML - create a html file say index.html
<button class="btn" onclick="getData()"> Show Data </button>
  1. CSS (Not Necessary, just to make button presentable. Can be skipped) index.css.
.btn{
    padding: 10px;
    border: none;
    color: #fff;
    background-color: green;
    cursor:pointer;
}
  1. JS - index.js

The complete code has been executed on this CodePen. Try consoling at different places and see the response in browser.

Guided Practice

Independent Practice

Points to Remember

Supplemental Materials

Check for Understanding

Make the simplest project you can on Codepen.io. The project must: