How to make an HTTP request in JavaScript

Configurare noua (How To)

Situatie

Solutie

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

Here’s an example using XMLHttpRequest:

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

Here’s an example using XMLHttpRequest:
const xhr = new XMLHttpRequest();
xhr.open(‘GET’, ‘https://example.com/api/data’);
xhr.send();

xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
console.log(xhr.responseText); // the response data
} else {
console.error(‘Request failed’);
}
}
};

Tip solutie

Permanent

Voteaza

(7 din 13 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?