How do make an HTTP request in Javascript?

To make an HTTP request in JavaScript, you can use the built-in fetch method or XMLHttpRequest object. Here's an example of how to use fetch to make a GET request:

fetch('https://example.com/data')

.then(response => response.json())

.then(data => console.log(data))

.catch(error => console.error(error));

In this example, fetch takes a URL as its first argument and returns a Promise that resolves to the response object. The response is then converted to JSON using the json method, and the resulting data is logged to the console.

You can also use fetch to make other types of requests, such as POST, PUT, DELETE, etc. by specifying additional options in the second argument, like this:

fetch('https://example.com/data', {

method: 'POST',

headers: {

'Content-Type': 'application/json'

},

body: JSON.stringify({ name: 'John Doe' })

})

.then(response => response.json())

.then(data => console.log(data))

.catch(error => console.error(error));

In this example, we're making a POST request and passing in some data as the request body in JSON format. The headers option specifies the type of data we're sending, and the JSON.stringify method is used to convert the data to a JSON string.