Skip to content

Latest commit

 

History

History
201 lines (166 loc) · 8.33 KB

04_ajax_fetch_api.md

File metadata and controls

201 lines (166 loc) · 8.33 KB

Tutorial: Fetching Data from Servers Using AJAX and Fetch API

Introduction

Fetching data from servers is a common requirement in web development, enabling you to dynamically load and update content without refreshing the entire page. This tutorial will cover how to fetch data from servers using AJAX (Asynchronous JavaScript and XML) and the Fetch API with business-related examples. By mastering these techniques, you can create dynamic and responsive web applications.

AJAX (Asynchronous JavaScript and XML)

AJAX is a set of web development techniques that allows web applications to communicate with servers asynchronously. Although XML was originally used, modern applications often use JSON (JavaScript Object Notation) for data exchange.

Using XMLHttpRequest

The XMLHttpRequest object is used to interact with servers and fetch data.

Example: Suppose you need to fetch sales data from a server and display it on the page.

<!DOCTYPE html>
<html>
<head>
    <title>AJAX Sales Data</title>
</head>
<body>
    <div id="salesData">Loading sales data...</div>
    <button id="fetchSalesButton">Fetch Sales Data</button>

    <script>
        let fetchSalesButton = document.getElementById('fetchSalesButton');
        let salesDataElement = document.getElementById('salesData');

        fetchSalesButton.addEventListener('click', () => {
            let xhr = new XMLHttpRequest();
            xhr.open('GET', 'https://api.example.com/sales');
            xhr.onreadystatechange = function() {
                if (xhr.readyState === 4 && xhr.status === 200) {
                    let salesData = JSON.parse(xhr.responseText);
                    salesDataElement.textContent = `Total Sales: $${salesData.total}`;
                }
            };
            xhr.send();
        });
    </script>
</body>
</html>

Explanation: In this example, an XMLHttpRequest object is used to fetch sales data from a server when the button is clicked. The onreadystatechange event handler checks if the request is complete and successful (readyState === 4 && status === 200). If so, the response data (in JSON format) is parsed and displayed. This demonstrates how to use AJAX to fetch and display data dynamically.

Fetch API

The Fetch API provides a modern, more powerful, and flexible way to make asynchronous requests and handle responses. It returns promises, making it easier to work with asynchronous code.

Using Fetch API

Example: Suppose you need to fetch product data from a server and display it on the page.

<!DOCTYPE html>
<html>
<head>
    <title>Fetch API Product Data</title>
</head>
<body>
    <div id="productData">Loading product data...</div>
    <button id="fetchProductButton">Fetch Product Data</button>

    <script>
        let fetchProductButton = document.getElementById('fetchProductButton');
        let productDataElement = document.getElementById('productData');

        fetchProductButton.addEventListener('click', () => {
            fetch('https://api.example.com/products')
                .then(response => response.json())
                .then(data => {
                    productDataElement.textContent = `Product Name: ${data.name}, Price: $${data.price}`;
                })
                .catch(error => {
                    console.error('Error fetching product data:', error);
                    productDataElement.textContent = 'Error loading product data';
                });
        });
    </script>
</body>
</html>

Explanation: In this example, the fetch function is used to fetch product data from a server when the button is clicked. The response is converted to JSON using response.json(), and the product name and price are displayed. The catch method handles any errors that occur during the fetch operation. This demonstrates how to use the Fetch API to fetch and display data dynamically.

Handling POST Requests with Fetch API

Example: Suppose you need to submit an order to the server using a POST request.

<!DOCTYPE html>
<html>
<head>
    <title>Submit Order</title>
</head>
<body>
    <form id="orderForm">
        <label for="productName">Product Name:</label>
        <input type="text" id="productName" name="productName" required>
        <label for="quantity">Quantity:</label>
        <input type="number" id="quantity" name="quantity" required>
        <button type="submit">Submit Order</button>
    </form>
    <div id="orderStatus"></div>

    <script>
        let orderForm = document.getElementById('orderForm');
        let orderStatus = document.getElementById('orderStatus');

        orderForm.addEventListener('submit', (event) => {
            event.preventDefault();
            let formData = new FormData(orderForm);
            let orderData = {
                productName: formData.get('productName'),
                quantity: formData.get('quantity')
            };

            fetch('https://api.example.com/orders', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(orderData)
            })
                .then(response => response.json())
                .then(data => {
                    orderStatus.textContent = 'Order submitted successfully!';
                })
                .catch(error => {
                    console.error('Error submitting order:', error);
                    orderStatus.textContent = 'Error submitting order';
                });
        });
    </script>
</body>
</html>

Explanation: In this example, an event listener is added to the orderForm to handle the form submission. The event.preventDefault() method is used to prevent the default form submission behavior. The form data is collected and converted into a JSON object. The fetch function is used to send a POST request to the server with the order data. The response is handled to display a success message, and errors are caught and logged. This demonstrates how to handle POST requests using the Fetch API.

Handling Response Status and Errors

Example: Suppose you need to fetch user data and handle different response statuses.

<!DOCTYPE html>
<html>
<head>
    <title>Fetch User Data</title>
</head>
<body>
    <div id="userData">Loading user data...</div>
    <button id="fetchUserButton">Fetch User Data</button>

    <script>
        let fetchUserButton = document.getElementById('fetchUserButton');
        let userDataElement = document.getElementById('userData');

        fetchUserButton.addEventListener('click', () => {
            fetch('https://api.example.com/user')
                .then(response => {
                    if (!response.ok) {
                        throw new Error(`HTTP error! status: ${response.status}`);
                    }
                    return response.json();
                })
                .then(data => {
                    userDataElement.textContent = `User Name: ${data.name}, Email: ${data.email}`;
                })
                .catch(error => {
                    console.error('Error fetching user data:', error);
                    userDataElement.textContent = 'Error loading user data';
                });
        });
    </script>
</body>
</html>

Explanation: In this example, the fetch function is used to fetch user data from a server when the button is clicked. The response is checked for success using response.ok. If the response is not OK, an error is thrown with the response status. The JSON data is then displayed if the response is successful. Errors are caught and logged, and an error message is displayed. This demonstrates how to handle different response statuses and errors using the Fetch API.

Conclusion

By understanding and using AJAX and the Fetch API in JavaScript through these business-related examples, you can create dynamic and responsive web applications that interact with servers to fetch and update data. Practice these concepts to build a strong foundation in JavaScript data fetching techniques.

Further Reading