How do I make an HTTP request in Javascript?

To make an HTTP request in JavaScript, you can use the XMLHttpRequest object or the newer fetch() function.

Here’s an example using XMLHttpRequest to make a GET request to the OpenAI API:

var xhr = new XMLHttpRequest();'GET', '');

xhr.onload = function() {
  if (xhr.status === 200) {


And here’s an example using fetch() to make the same request:

  .then(response => response.text())
  .then(data => console.log(data))
  .catch(error => console.error(error))

Both of these examples will make an HTTP GET request to the specified URL and log the response to the console.

Note that these examples only cover simple GET requests. You can use the XMLHttpRequest object or the fetch() function to make other types of HTTP requests, such as POST, PUT, and DELETE, as well as send data in the request body and set custom headers. You can find more information in the documentation for XMLHttpRequest and fetch().