Send GET Request using JavaScript

XMLHttpRequest object

const xmlHttp = new XMLHttpRequest();

xmlHttp.onreadystatechange = () => {
    if (xmlHttp.readyState === 4 && xmlHttp.status === 200) {
        const content = xmlHttp.responseText;

        console.log(content);
    }
}

xmlHttp.open('GET', 'https://httpbin.org/get');
xmlHttp.send();

Fetch API

fetch('https://httpbin.org/get', { method: 'GET' })
    .then(response => response.text())
    .then(content => {
        console.log(content);
    });

Leave a Comment

Cancel reply

Your email address will not be published.