You can make an HTTP request in JavaScript using the built-in fetch()
function or the older XMLHttpRequest
object. Here’s an example using fetch()
:
fetch(‘https://example.com/data.json’)
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));
This code fetches JSON data from https://example.com/data.json
, parses the response using the .json()
method, logs the resulting data to the console, and catches any errors that occur during the process. Note that fetch()
returns a Promise, so you can chain then()
and catch()
methods to handle the response and any errors.