← Week 4 — 23 / 24 →

AJAX and REST APIs

Using XMLHttpRequest

JSON

Think of a Request as an object you are building, like a little boat or a wind-up toy. You can set its properties, like the URL and the HTTP method, and then you can send it off to the server. When the server responds, you can handle the response in a callback function.

function reqListener() {
  console.log(this.responseText);
}

const req = new XMLHttpRequest();
req.addEventListener("load", reqListener);
req.open("GET", "http://www.example.org/example.txt");
req.send();