Integrating API Requests for Dynamic Tutor Interaction with ASP.NET Core

Integrating API Requests for Dynamic Tutor Interaction

Welcome back! In the previous lesson, you set up a basic interface for your personal tutor using ASP.NET Core Razor Pages and HTML. This laid the foundation for creating a user-friendly educational web application. Now, we will take the next step by connecting this interface to our backend API. This connection is crucial for transforming our static interface into a dynamic, interactive tutoring experience. By the end of this lesson, you will understand how to integrate the frontend with the backend, enabling real-time communication between the student and the tutor system.

Understanding Fetch API

In this lesson, we will enhance our tutor interface by connecting it to the backend API using the Fetch API. This tool will allow us to capture student input and send it to the server, transforming our tutor into a dynamic application.

The Fetch API is a modern interface that allows you to make network requests from your web page. It provides a more powerful and flexible feature set for handling HTTP requests and responses. With Fetch, you can send and receive data from the server in the background, creating a smoother and more interactive educational experience.

Here's how it works:

  1. Making a Request: When you want to get or send data, you use the fetch() function. You tell it where to send the request (the URL) and what kind of request it is.
  2. Handling the Response: After the request is sent, the server will reply. The Fetch API lets you handle this reply using .then(). You can think of this as receiving an answer from your tutor.
  3. Dealing with Errors: Sometimes things go wrong, like if the server is down. The Fetch API lets you handle these problems using .catch(), so your web page can show a message or try again.

Here is a simple example of how the Fetch API works within a function:

function fetchData() {
    fetch('/api/example_endpoint', { method: 'GET' })
        .then(r => r.json())
        .then(data => {
            console.log('Success:', data);
        })
        .catch(error => {
            console.error('Error occurred:', error);
        });
}

Initializing Tutor Variables

Now that we have a basic understanding of the Fetch API, let's prepare our tutor application by initializing some variables to store the current session and user IDs. These variables will help us manage and track each tutoring session as we implement dynamic functionalities.

<script>
    // Get references to the messages container and input
    const messagesContainer = document.getElementById('messages');
    const messageInput      = document.getElementById('message-input');

    // Store the current tutoring session ID
    let currentSessionId = null;
    let userId = '12345';
</script>
Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal