Chapter 17. Using Asynchronous Communication

The term Ajax was first coined in 2005. It stands for Asynchronous JavaScript and XML, which, in simple terms, means using a set of methods built into JavaScript to transfer data between the browser and a server in the background. This term has now been mostly abandoned in favor of simply talking about asynchronous communication, and one of the reasons is that these days, programmers are more likely to use JavaScript Object Notation (JSON) as their preferred data-interchange format, as it’s a simple subset of JavaScript.

An excellent example of this technology is Google Maps (although there are numerous others), in which new sections of a map are downloaded from the server when needed, without requiring a page refresh.

Using asynchronous communication not only substantially reduces the amount of data that must be sent back and forth but also makes web pages seamlessly dynamic—allowing them to behave more like self-contained applications. The results are a much improved user interface and better responsiveness.

The Fetch API

In the past, making Ajax calls was a real pain in the neck because there were so many different implementations across various browsers. Luckily things vastly improved around 2015, when the simple fetch function was introduced to modern browsers. It is defined by the Fetch standard, which unifies how requests and responses work across the whole browser.

So, for example, to make a GET request, you use code such as ...

Get Learning PHP, MySQL & JavaScript, 7th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.