JS code in django project

In a JS code I’m using fetch to make GET requests to a url in order to obtain data in JSON format. The js code is located in my Django project and I use it in a template. When I click on the button that activates the js script, it tells me that there is an error in the fetch, can someone help me? This is the javascript code:

function iniciarEscaneo() {
    // Mostrar el mensaje de escaneo en progreso
    document.getElementById("mensaje").innerText = "Escaneando NFC...";
    document.getElementById("mensaje").style.display = "block";
    
    // Establecer tiempo límite de espera en 10 segundos (10000 milisegundos)
    var tiempoLimite = 10000;
    
    // Realizar la solicitud GET a la URL para obtener el NFC
    fetch("http://192.168.0.23:8081/read_nfc")
        .then(response => response.json())
        .then(data => {
            // Procesar la respuesta JSON
            if (data && data.uid) {
                // Actualizar el campo UID NFC en la plantilla con el UID obtenido
                document.getElementById("mensaje").innerText = "NFC leído correctamente.";
                document.getElementById("id_uid_nfc").innerText = data.uid;
            } else {
                document.getElementById("mensaje").innerText = "No se pudo leer el NFC.";
            }
        })
        .catch(error => {
            console.error('Error al obtener el NFC:', error);
            document.getElementById("mensaje").innerText = "Error al obtener el NFC: " + error.message;
        })
        .finally(() => {
            // Ocultar el mensaje después de un breve período de tiempo
            setTimeout(function() {
                if (document.getElementById("mensaje").innerText === "Escaneando NFC...") {
                    document.getElementById("mensaje").innerText = "Tiempo de espera excedido. Intenta nuevamente.";
                }
                document.getElementById("mensaje").style.display = "none";
            }, tiempoLimite);
        });
}

The error message that I get is: Error al obtener el NFC: failed to fetch

As a start, you’ll need to post the full error message you have received. (Both the error as it is reported in your JavaScript, along with any error and traceback reported on your server’s console.

1 Like

also look at your network tab, it will indicate whether your server response at http://192.168.0.23:8081/read_nfc is a 200, a 404 … or a 500 :slight_smile: (or even something else!)

In any case, look at your server logs in parallel to your client logs. It will help you determine what’s happening…