0 points

js-sendGetRequest


dashboard

Sun Oct 18 2020 17:03:56 GMT+0000 (UTC)

Posted by @aymanelya

const httpGet = (url, callback, err = console.error) => {
  const request = new XMLHttpRequest();
  request.open('GET', url, true);
  request.onload = () => callback(request.responseText);
  request.onerror = () => err(request);
  request.send();
};

httpGet(
  'https://jsonplaceholder.typicode.com/posts/1',
  console.log
); 

// Logs: {"userId": 1, "id": 1, "title": "sample title", "body": "my text"}
content_copy Copy

https://blog.madza.dev/24-modern-es6-code-snippets-to-solve-practical-js-problems?guid