HTTP request in Javascript
There are several ways to make an HTTP request in JavaScript, but the most common method is to use the XMLHttpRequest object or the fetch() function.
Here's an example of how to use XMLHttpRequest to make a GET request to a server:
ar xhr = new XMLHttpRequest(); xhr.open("GET", "https://example.com", true); xhr.onreadystatechange = function() { if (xhr.readyState === 4 && xhr.status === 200) { console.log(xhr.responseText); } }; xhr.send();
And here's an example of how to use fetch() to make a GET request:
fetch("https://example.com") .then(function(response) { return response.text(); }) .then(function(text) { console.log(text); });
For further information on how to make different types of requests and handle responses, you can refer to the Mozilla Developer Network (MDN) documentation for XMLHttpRequest and fetch().
Comments
Post a Comment