Files
30-seconds-of-code/snippets/httpGet.md
Stefan Feješ fad6c9430c update httpGet
2018-01-10 19:43:29 +01:00

618 B

httpGet

Makes a GET request to the passed URL.

Use XMLHttpRequest web api to make a get request to the given url. Handle the onload event, by console logging the responseText. Handle the onerror event, by running the provided err function.

const httpGet = (url, err = console.error) => {
  const request = new XMLHttpRequest();
  request.open("GET", url, true);
  request.onload = () => console.log(request.responseText);
  request.onerror = () => err(request);
  request.send();
};
httpGet('https://jsonplaceholder.typicode.com/posts'); // 'Console logs JSON of 100 posts'