</>
Skip to content
jQuery lessons (28/39)

jQuery — AJAX

$.ajax()

The core AJAX method:

$.ajax({
    url: "https://api.example.com/data",
    method: "GET",
    success: function(data) {
        console.log(data);
    },
    error: function(xhr, status, error) {
        console.log("Error:", error);
    }
});

$.get()

$.get("https://api.example.com/users", function(data) {
    console.log(data);
});

$.post()

$.post("https://api.example.com/users", {
    name: "John",
    email: "john@example.com"
}, function(data) {
    console.log("Created:", data);
});

$.getJSON()

$.getJSON("https://api.example.com/data.json", function(data) {
    console.log(data);
});

load()

Load HTML into an element:

$("#content").load("page.html");
$("#content").load("page.html #main");

AJAX Events

$(document).ajaxStart(function() {
    $("#loading").show();
});

$(document).ajaxStop(function() {
    $("#loading").hide();
});

Error Handling

$.ajax({
    url: "/api/data",
    error: function(xhr, status, error) {
        if (status === "timeout") {
            console.log("Request timed out");
        }
    }
});

Mini Practice

  1. Make a GET request to a public API
  2. POST data to a server
  3. Load HTML into a div
  4. Show a loading indicator during requests

Up Next

Continue with Load — loading HTML content into elements.

Related Topics

Frequently Asked Questions about AJAX

What is AJAX in jQuery?

AJAX is a fundamental concept in jQuery. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn AJAX?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn AJAX.

Why is AJAX important in jQuery?

AJAX is essential for jQuery development. Understanding this concept will help you write better code and solve real-world problems more effectively.