Default (GPT-3.5)

How do I make an HTTP request in Javascript?

ChatGPTChatGPT

You can make an HTTP request in Javascript using the `XMLHttpRequest` object or the newer `fetch` API. Here is an example of how to make a GET request using both methods:

Using XMLHttpRequest:

var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://api.example.com/data', true);
xhr.onreadystatechange = function() {
  if (xhr.readyState === 4 && xhr.status === 200) {
    var data = JSON.parse(xhr.responseText);
    console.log(data);
  }
};
xhr.send();

Using fetch:

fetch('https://api.example.com/data')
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.error('Error:', error));

Both methods allow you to perform asynchronous requests and handle the response data accordingly.