Create and Open Request
As its name implies, XMLHttpRequest is a JavaScript object, and the first step in using it is to create a new instance of it:
let xhr = new XMLHttpRequest();
With a new instance created, we can use the .open(method[String],url[String],async[Boolean]) method to initialize the request.
-
method: is a string that corresponds to one HTTP method ('GET', 'POST', 'PUT', or 'DELETE') -
url: is a string of the URL to send the request to. -
async: an optional boolean (defaults totrue) indicating whether or not to perform the XMLHttpRequest asynchronously.
let xhr = new XMLHttpRequest();
// Asynchronous request (defaults to true)
xhr.open('GET', 'http://someUrl.com/resource', true);
// Synchronous request
xhr.open('GET', 'http://someUrl.com/resource', false);
Skill++¶
We will never send XHR requests synchronously, which would freeze our browser until the request completes. Instead we will usually just omit the third argument to
open, letting it default totruefor an asynchronous request.