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 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 response = JSON.parse(xhr.responseText);
    console.log(response);
  }
};
xhr.send();

And here is an example using the `fetch` API:

fetch('https://api.example.com/data')
  .then(response => {
    return response.json();
  })
  .then(data => {
    console.log(data);
  });

Both of these examples make a GET request to https://api.example.com/data and log the response data to the console. You can modify them to make other types of requests (e.g., POST, PUT, DELETE) by changing the method parameter in `open()` for `XMLHttpRequest`, or by passing additional options in the fetch call.