← Back to jQuery Course | Chapter 9: AJAX Basics | Lesson 1 of 7

Introduction to AJAX

AJAX lets a page request data from a server without reloading the whole page. jQuery makes AJAX tasks easier.

What is AJAX?

AJAX lets a page request data from a server in the background without a full page reload; jQuery wraps the browser's raw AJAX API in a simpler, more consistent one. This is what makes features like live search or infinite scroll possible without navigating away.

Example: What is AJAX?

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <button id="btn">Load</button>
    <script>
      $("#btn").on("click", function() {
      $.ajax({url: "data.php", success: function(data) {
      console.log("Received:", data);
      }});
      });
    </script>
  </body>
</html>

AJAX Method

The $.ajax() method accepts a configuration object with options such as url, method, data, success, and error, giving you full control over exactly how the request is made. It's the most flexible AJAX method jQuery provides, underlying the shorthand methods like get() and post().

Example: AJAX Method

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <script>
      $.ajax({
      url: "data.php",
      method: "GET",
      success: function(data) { console.log(data); },
      error: function() { console.log("Request failed"); }
      });
    </script>
  </body>
</html>

Handling Returned Data

A successful request passes its returned data to the success callback, where you can use it to update the page -- inserting it into the DOM, filling a table, or updating a counter. What that data looks like depends entirely on what the server sends back.

Example: Handling Returned Data

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <ul id="list"></ul>
    <script>
      $.ajax({url: "data.php", success: function(data) {
      $("#list").append("<li>" + data.message + "</li>");
      }});
    </script>
  </body>
</html>

AJAX Error Handling

Network requests can fail for many reasons -- a dropped connection, a server error, a bad URL -- so the error callback lets you show a friendly message instead of leaving the page silently broken. Handling this case is just as important as handling success.

Example: AJAX Error Handling

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <script>
      $.ajax({
      url: "data.php",
      success: function(data) { console.log(data); },
      error: function() { console.log("Something went wrong, please try again"); }
      });
    </script>
  </body>
</html>

AJAX in a Small Project

AJAX is useful for small interactive pages where loading everything up front would be wasteful, like fetching extra details only when the user clicks a 'show more' button. This keeps the initial page load light while still allowing rich, on-demand interactivity.

Example: AJAX in a Small Project

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <button id="showMore">Show More</button>
    <div id="details"></div>
    <script>
      $("#showMore").on("click", function() {
      $.ajax({url: "data.php", success: function(data) {
      $("#details").text(data.message);
      }});
      });
    </script>
  </body>
</html>

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.