TimesCoding

Learn, Practice, and Excel with our Coding Tutorials!

How to make an HTTP request in Javascript?

January 30, 2023 1 Min read JAVASCRIPT

You can make an HTTP request in JavaScript using the ‘XMLHttpRequest’ or ‘fetch’ API:

Using ‘XMLHttpRequest’:

var xhr = new XMLHttpRequest();
xhr.open("GET", "https://www.example.com", true);
xhr.onreadystatechange = function() {
  if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
    console.log(this.responseText);
  }
};
xhr.send();

Using ‘fetch’ API:

fetch('https://www.example.com')
  .then(response => response.text())
  .then(data => console.log(data))
  .catch(error => console.error(error));
Related Post:
Create a Stylish Analog Watch with HTML, CSS, and JavaScript − Creating a stylish analog watch using HTML, CSS, and JavaScript involves simulating the appearance and movement of clock hands. We’ll build a simple analog clock.
How to Create a Stylish Digital Watch with HTML, CSS, and JavaScript − Creating a digital watch similar to an Apple Watch using HTML, CSS, and JavaScript involves several steps. We’ll build a simple, functional digital clock that.
How to Create a Colorful Tic-Tac-Toe Game with HTML, CSS and JavaScript − Here’s a colorful Tic-Tac-Toe game implemented using HTML, CSS, and JavaScript only. The game includes a restart button, winner message, and special effects for the.
Hide Show Mobile Number Using HTML, CSS, and JavaScript − In this example, the mobile number is initially displayed with the first few digits visible, and when clicked, the full mobile number is revealed. The.