In JavaScript, you can make HTTP requests using the built-in XMLHttpRequest object or the more modern fetch API. Here's how you can use both methods to make HTTP GET requests:
- Using the
XMLHttpRequestobject: - var xhr = new XMLHttpRequest(); xhr.open("GET", "https://api.example.com/data", true); xhr.onreadystatechange = function () { if (xhr.readyState === 4 && xhr.status === 200) { var response = xhr.responseText; console.log(response); } }; xhr.sen d();
- Using the
fetchAPI (modern approach):
javascriptfetch("https://api.example.com/data")
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json();
})
.then(data => {
console.log(data);
})
.catch(error => {
console.error("Fetch error:", error);
});
The fetch API returns a Promise that resolves with the Response to the request. You can use .then() to process the response and handle errors.
For more advanced features like sending data, setting headers, or making POST requests, you can refer to the documentation for each approach:
XMLHttpRequest: MDN Web Docs - Using XMLHttpRequestfetchAPI: MDN Web Docs - Fetch API
Additionally, keep in mind that there are also third-party libraries like Axios and jQuery that provide more advanced features and a simpler API for making HTTP requests.

0 on: "How do I make an HTTP request in Javascript?"