Saturday, 9 April 2022

How do I make an HTTP request in Javascript?

 In JavaScript, you can make an HTTP request using the built-in XMLHttpRequest object or the newer fetch() API. Here's how to use each one:

Using XMLHttpRequest:

var xhttp = new XMLHttpRequest();

xhttp.onreadystatechange = function() {

  if (this.readyState == 4 && this.status == 200) {

    console.log(this.responseText);

  }

};

xhttp.open("GET", "http://example.com/api/data", true);

xhttp.send();
Read more »

Labels: