How do I make an HTTP request in Javascript

In JavaScript, you can make an HTTP request using the XMLHttpRequest object or the newer fetch() function. Here’s an example of how to make an HTTP GET request using both methods:

Using XMLHttpRequest:

var xhr = new XMLHttpRequest();"GET", "", true);
xhr.onreadystatechange = function() {
  if (xhr.readyState === 4 && xhr.status === 200) {
    var response = JSON.parse(xhr.responseText);
    // Process the response data

Using fetch():

  .then(function(response) {
    if (response.ok) {
      return response.json();
    throw new Error("HTTP status code: " + response.status);
  .then(function(data) {
    // Process the response data
  .catch(function(error) {
    // Handle any errors

Both methods allow you to send additional data, headers, and handle different HTTP methods (GET, POST, PUT, DELETE, etc.). However, the fetch() function has a simpler and more modern API and is generally recommended for making HTTP requests in modern JavaScript applications.