To make an HTTP GET request in JavaScript, you can use the XMLHttpRequest object or fetch API. Here's an example using the XMLHttpRequest object:
index.tsx260 chars14 lines
In the above code block, we create a new XMLHttpRequest object and specify the method (GET), URL, and whether or not the request should be asynchronous (true). We then define a callback function for the onload
event, which is triggered when the response is received. If the response status is 200 (OK), we parse the response text as JSON data and log it to the console.
Alternatively, you can use the fetch API to make an HTTP GET request:
index.tsx133 chars6 lines
In this code block, we call the fetch
method with the URL as an argument. This returns a Promise that resolves to the response. We then use the json
method to parse the response body and return another Promise that resolves to the JSON data. Finally, we log the data to the console.
gistlibby LogSnag