gistlib
Here are four ways to make a GET request in JavaScript:
index.tsxconst xhr = new XMLHttpRequest(); xhr.open('GET', 'https://jsonplaceholder.typicode.com/posts', true); xhr.onload = () => { if (xhr.status === 200) { console.log(xhr.responseText); } else { console.log('Error Occurred'); } } xhr.send(); 251 chars11 lines
const xhr = new XMLHttpRequest(); xhr.open('GET', 'https://jsonplaceholder.typicode.com/posts', true); xhr.onload = () => { if (xhr.status === 200) { console.log(xhr.responseText); } else { console.log('Error Occurred'); } } xhr.send();
index.tsxfetch('https://jsonplaceholder.typicode.com/posts') .then(response => response.json()) .then(data => console.log(data)) .catch(error => console.error(error)); 165 chars5 lines
fetch('https://jsonplaceholder.typicode.com/posts') .then(response => response.json()) .then(data => console.log(data)) .catch(error => console.error(error));
index.tsx$.get('https://jsonplaceholder.typicode.com/posts', (data) => console.log(data)) 81 chars2 lines
$.get('https://jsonplaceholder.typicode.com/posts', (data) => console.log(data))
index.tsxaxios.get('https://jsonplaceholder.typicode.com/posts') .then(response => console.log(response.data)) .catch(error => console.error(error)); 145 chars4 lines
axios.get('https://jsonplaceholder.typicode.com/posts') .then(response => console.log(response.data)) .catch(error => console.error(error));
gistlibby LogSnag