This code sends a GET request to the specified URL (https://api.example.com/data). When the response is received and its status is 200 (OK), the onreadystatechange callback is triggered, and you can process the response.
Using the fetch API (more modern and recommended :
The fetch function returns a promise that resolves to the Response object. You can use the ok property of the Response object to check if the request was successful. If it was, you can call the json() method on the Response object to parse the response data as JSON.
These examples demonstrate how to make a simple GET request, but you can also make other types of requests (e.g., POST, PUT, DELETE) by specifying additional parameters in the open method or the fetch function.