Although the XMLHttpRequest object ruled AJAX communication for a long time, modern JavaScript
introduces a much better, shorter, and more elegant replacement: the Fetch API. Fetch utilizes
what we call Promises, allowing us to write much cleaner code without the hassle of complex state
tracking (readyState).
The fetch() method takes only one required parameter: the path to the data (URL). Because it works
with promises, we chain the responses using the .then() method:
fetch("alphabet.php?q=A")
.then(response => response.text()) // Converts raw response to text
.then(data => {
document.getElementById("output").innerHTML = data;
});
Main Advantage: The code is highly readable, contains no complex event listeners, and automatically supports modern data formats like JSON.