JavaScript
How to move image or element with JavaScript
January 04, 2023
1 min
There are several ways to make an HTTP request in JavaScript. Here are a few options:
XMLHttpRequest
object: This is a built-in object in JavaScript that allows you to make HTTP requests. Here’s an example of how to use it:var xhr = new XMLHttpRequest(); xhr.open('GET', 'https://example.com/api/endpoint'); xhr.onload = function () { if (xhr.status === 200) { console.log(xhr.responseText); } }; xhr.send();
fetch
function: This is a newer way to make HTTP requests that is supported by modern browsers. It returns a Promise that resolves to the response of the request. Here’s an example of how to use it:fetch('https://example.com/api/endpoint') .then(response => response.text()) .then(data => console.log(data));
I hope this helps! Let me know if you have any questions.