Menu

gitpiper

httpGet javascript Code Snippet in 2024

browserintermediate

Last Updated: 12 April 2024

Makes a GET request to the passed URL.

  • Use the XMLHttpRequest web API to make a GET request to the given url.
  • Handle the onload event, by calling the given callback the responseText.
  • Handle the onerror event, by running the provided err function.
  • Omit the third argument, err, to log errors to the console's error stream by default.
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": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto" } */

javascript snippet similar to httpGet For You in April 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️