Going Further Lesson 9 of 10
jQuery AJAX — Loading and Sending Data Without Reloading
Load and send data in the background with jQuery AJAX — $.getJSON, $.ajax, $.post and serialize — handle loading states and errors, and see the modern fetch equivalent.
AJAX lets a page talk to the server in the background: load new posts, check if a username is taken, or save a form without reloading the whole page. jQuery made AJAX easy, and you'll still see its AJAX code in many existing systems.
$.getJSON: load data
$.getJSON("api/products.php", function (products) {
const list = $("#products").empty();
products.forEach(function (p) {
list.append($("<li>").text(p.name + " – ₱" + p.price));
});
});
The server (for example a PHP file) returns JSON like [{"name":"Pandesal","price":25}, ...], and jQuery parses it into a JavaScript array for you.
$.ajax: full control
$.ajax({
url: "api/grades.php",
method: "GET",
data: { section: "Rizal" }, // becomes ?section=Rizal
dataType: "json"
})
.done(function (grades) {
$("#count").text(grades.length + " students loaded");
})
.fail(function (xhr) {
$("#count").text("Error " + xhr.status + ": could not load grades.");
})
.always(function () {
$("#spinner").hide();
});
.done()runs on success..fail()runs on errors (server down, 404, invalid JSON)..always()runs either way, perfect for hiding a loading spinner.
Sending a form
$("#contactForm").on("submit", function (e) {
e.preventDefault();
$.post("api/contact.php", $(this).serialize())
.done(function (res) { $("#msg").text("Salamat! Message sent."); })
.fail(function () { $("#msg").text("Sending failed. Please try again."); });
});
.serialize() turns all named form fields into name=Ana&email=ana%40mail.com, the same format a normal form submission uses, so the PHP side reads it with $_POST.
Try it: a simulated server
The Try-it editor can't reach a real server, so this example fakes one with a short delay. Your code handles it exactly as it would a real response:
<script src="/vendor/jquery.min.js"></script>
<button id="load">Load sections</button>
<button id="broken">Load (server error)</button>
<p id="status"></p>
<ul id="list"></ul>
<script>
// Pretend server: returns a jQuery promise like $.ajax does
function fakeAjax(fail) {
const d = $.Deferred();
setTimeout(function () {
if (fail) d.reject({ status: 500 });
else d.resolve([
{ name: "Rizal", students: 38 },
{ name: "Bonifacio", students: 40 },
{ name: "Mabini", students: 35 }
]);
}, 900);
return d.promise();
}
function load(fail) {
$("#list").empty();
$("#status").text("⏳ Loading…");
fakeAjax(fail)
.done(function (sections) {
sections.forEach(function (s) {
$("#list").append($("<li>").text(s.name + " (" + s.students + " students)"));
});
$("#status").text("✅ Loaded " + sections.length + " sections.");
})
.fail(function (xhr) {
$("#status").text("❌ Error " + xhr.status + ". Please try again later.");
});
}
$("#load").on("click", function () { load(false); });
$("#broken").on("click", function () { load(true); });
</script>
The modern way: fetch
Plain JavaScript now has fetch, which does the same job without jQuery:
fetch("api/products.php")
.then((res) => {
if (!res.ok) throw new Error("HTTP " + res.status);
return res.json();
})
.then((products) => console.log(products))
.catch((err) => console.error(err));
New projects usually use fetch; existing jQuery projects use $.ajax. Knowing both lets you work on either.
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04