In JavaScript, you can make HTTP requests using the built-in XMLHttpRequest object or the newer fetch API. Here are examples of how to use both of these methods:

Using XMLHttpRequest:

const xhr = new XMLHttpRequest();
xhr.open('GET', 'https://jsonplaceholder.typicode.com/todos/1');
xhr.onload = function() {
  if (xhr.status === 200) {
    console.log(xhr.responseText);
  } else {
    console.error('Request failed.  Returned status of ' + xhr.status);
  }
};
xhr.send();

Using fetch:

fetch('https://jsonplaceholder.typicode.com/todos/1')
  .then(response => {
    if (response.ok) {
      return response.json();
    } else {
      throw new Error('Request failed.');
    }
  })
  .then(data => console.log(data))
  .catch(error => console.error(error));

Both of these examples make a GET request to the same URL and log the response data to the console. You can modify the method, URL, and request data to make different types of requests.

Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *