To make an HTTP request in JavaScript, you can use the built-in XMLHttpRequest object or the fetch() function. Here are examples using both methods:

Using XMLHttpRequest:

var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://example.com/api/data');
xhr.onload = function() {
  if (xhr.status === 200) {
    console.log(xhr.responseText);
  } else {
    console.log('Request failed.  Returned status of ' + xhr.status);
  }
};
xhr.send();

Using fetch():

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

Both methods allow you to make HTTP requests and handle the response data in different ways. It's up to you to choose which method is best for your application.

How-do-I-make-an-HTTP-request-in-Javascript

原文地址: https://www.cveoy.top/t/topic/qHr 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录