Chapter 4

Network Requests

Many browser APIs are asynchronous in nature. This chapter contains recipes for performing basic tasks asynchronously using callback functions, events, and Promises.

Examples

Recipe 4.1
Sending a Request With XMLHttpRequest

Use the XMLHttpRequest API to send a GET request to a web API.

Recipe 4.2
Sending a GET Request With the Fetch API

Use the Fetch API to send a GET request to a web API.

Recipe 4.3
Sending a POST Request With the Fetch API

Use the Fetch API to send a POST request to a web API.

Recipe 4.4
Uploading a File with the Fetch API

Use the Fetch API to upload file data.

Recipe 4.5
Sending a Beacon

Send a quick POST request containing analytics or other data as the user is leaving your page.

Recipe 4.6
Listening for Remote Events with Server-Sent Events (SSE)

Send a quick POST request containing analytics or other data as the user is leaving your page.

Recipe 4.7
Exchanging Data in Real Time with WebSockets

Send a quick POST request containing analytics or other data as the user is leaving your page.

Web API Cookbook
Joe Attardi