Both XMLHttpRequest and fetch() are great tools for making Ajax calls, but fetch() is generally considered the better option for most applications. So if you’re looking for a way to make Ajax calls in JavaScript, give fetch() a try. We think you’ll be pleasantly surprised by the results!

Make HTTP Request in JavaScript

Here is an example of using XMLHttpRequest to make a GET request to retrieve data from remote API:

Here is an example of using fetch() to make a GET request to retrieve data from a remote API.

Both examples assume that you have an API key stored in a variable called API_KEY. You can also use fetch() to make other types of HTTP requests, such as POST, PUT, and DELETE, by passing an options object as the second argument. For example:

Note that XMLHttpRequest and fetch() are asynchronous, which means that the code will not block while the request is being made. Instead, the response will be processed in a callback function or with a promise.