Вы можете сделать HTTP-запрос в JavaScript, используя встроенный объект XMLHttpRequest или более новый метод fetch.
Использование XMLHttpRequest:
const xhr = new XMLHttpRequest();
xhr.open('GET', 'https://example.com/api/data');
xhr.onload = () => {
if (xhr.status === 200) {
console.log(xhr.responseText);
} else {
console.log('Request failed. Returned status of ' + xhr.status);
}
};
xhr.send();
Использование выборки:
fetch('https://example.com/api/data')
.then(response => response.text())
.then(data => console.log(data))
.catch(error => console.error(error));
Оба этих метода можно использовать для GET, POST, PUT, DELETE и других методов HTTP, изменив первый параметр метода open или используя параметр method в вызове fetch.