There are no items in your cart
Add More
Add More
Item Details | Price |
---|
To make an HTTP request in JavaScript, you can use the XMLHttpRequest
object or the newer fetch()
API. Here is an example of how to use XMLHttpRequest
to make a GET request to fetch a resource from a server:
// Create a new XMLHttpRequest object const xhr = new XMLHttpRequest();// Set the HTTP method to GET, the URL to fetch, and specify that the request is asynchronousxhr.open("GET", "https://www.example.com/resource", true);// Set a function to be called when the request is completexhr.onload = function() {// Check the status code to make sure the request was successfulif (this.status == 200) {// If successful, do something with the response data var data =JSON.parse(this.responseText);// ...} };// Send the requestxhr.send();
Here is an example of how to use the fetch()
API to make a GET request:
fetch("https://www.example.com/resource").then(response => response.json()).then(data => {// Do something with the response data // ...});
Both XMLHttpRequest
and fetch()
allow you to specify additional options and customize the request, such as by setting request headers or providing a request body for POST requests. Consult the documentation for more information.
DCT Academy
Full Stack Development Training Institute