AJAX is a developer’s dream because it enables:
AJAX Example Explained
HTML Page
<!DOCTYPE html> <div id=”demo”> </body> |
The HTML page includes a <div> section for displaying information from a server and a <button> that triggers a function when clicked.
This function fetches data from a web server and updates the <div> with the retrieved information.
Function loadDoc()
function loadDoc() { |
What is AJAX?
AJAX (Asynchronous JavaScript and XML) is not a programming language but a technique that combines:
The name “AJAX” can be misleading; while it suggests using XML for data transport, AJAX applications often use plain text or JSON for data exchange just as commonly. |
AJAX enables web pages to update asynchronously by exchanging data with a web server in the background, allowing specific parts of a page to be refreshed without reloading the entire page.
1. An event happens on a web page, such as the page loading or a button being clicked. |
2. JavaScript creates an XMLHttpRequest object. |
3. The XMLHttpRequest object sends a request to the web server. |
4. The server handles the request. |
5. The server returns a response to the web page. |
6. JavaScript reads the response. |
7. JavaScript performs the appropriate action, such as updating the page. |
Modern browsers support the Fetch API as an alternative to the XMLHttpRequest
object.
The Fetch API provides a simpler interface for making HTTP requests to web servers, performing the same tasks as XMLHttpRequest
with greater ease.