Skip to Content
Learning from jQuery
book

Learning from jQuery

by Callum Macrae
January 2013
Beginner to intermediate
116 pages
2h 15m
English
O'Reilly Media, Inc.
Content preview from Learning from jQuery

Chapter 4. AJAX

One of jQuery’s most used features is its suite of AJAX functions. They offer some significant improvements over the native JavaScript AJAX features, as they are a lot easier to use. AJAX is the act of making an HTTP request from JavaScript without having to reload the page; you could think of it as an inline HTTP request. Sometimes, however, it isn’t worth loading the entire jQuery library to send a few requests and nothing else, or it doesn’t provide enough control. It’s also useful to know how jQuery does it, as it can help you when you’re trying to debug your code. jQuery’s $.ajax function is also a massive 379 lines long at the time of writing.

In this chapter, we will cover the basics of AJAX, explore a bit about designing a website to use AJAX, and then AJAXify an example piece of code. This chapter contains a bit of PHP, but PHP knowledge isn’t strictly necessary—it’s not complicated PHP and I will be explaining it along the way.

Sending an AJAX Request

To send an AJAX request in jQuery, we use this very simple syntax:

$.get('/ajax/?foo=bar', function (data) {
        console.log(data); // The response
});

Sending an AJAX request in JavaScript alone is a lot more complicated. To send an AJAX request, we use the XMLHttpRequest object (or ActiveXObject in older versions of Internet Explorer). Here is an example of a basic AJAX request:

if (window.XMLHttpRequest) {
        var req = new XMLHttpRequest();
} else {
        // Internet Explorer
        var req = new ActiveXObject('Microsoft.XMLHTTP' ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Beyond jQuery

Beyond jQuery

Ray Nicholus
jQuery Cookbook

jQuery Cookbook

Cody Lindley
Learning jQuery 3 - Fifth Edition

Learning jQuery 3 - Fifth Edition

Adam Boduch, Jonathan Chaffer, Karl Swedberg

Publisher Resources

ISBN: 9781449335182Errata Page