API Authentication in JS

var xhr = new XMLHttpRequest();
xhr.open('GET', '/server', true);

xhr.onload = function () {
  // Do something with the retrieved data ( found in xmlhttp.response )
};

xhr.send(null);

xhr.send() doesn’t return anything

The response is in onload

Another way:

  xhr.onreadystatechange = function() {
    if (xhr.readyState === 4) {
      callback(xhr.response);
    }
  }
1 Like