I have a webpage that fetches information about products from a JSON file in my computer. The problem is that when .json()
gets executed, I get the error SyntaxError: Unexpected end of input
. This only happens if I run the webpage directly in the file system (opening the .html file) while, in the other hand, if I run the webpage in a server It works properly. Someone can tell me why and how to fix this issue?
async function obtener (url) {
if (typeof(url) !== 'string') return
const respuesta = await fetch(url, {
mode: "no-cors",
headers: {
"Content-Type": "application/json",
}
})
return respuesta.json() // error is thrown here
}
const productosURL = './datos/merchandising.json'
const productos = await obtener(productosURL)
JSON file:
[
{
"id": "ee154f46-10d4-4007-9ecf-44332bc45346",
"nombre": "Camiseta Prematch - 2023",
"categoria": "Hombre Fútbol",
"precio": 11000,
"disponible": true,
"imgURL": "./recursos/img/productos/futbolprematch2023.jpg"
},
{
"id": "4d20afcc-516d-4118-8b34-6cd17c17772c",
"nombre": "Camiseta Titular - 2023",
"categoria": "Hombre Fútbol",
"precio": 15000,
"disponible": true,
"imgURL": "./recursos/img/productos/futbol-titular2023.jpg"
},
{
"id": "778f7c59-4e9d-45a6-8b2e-2f9cb466476b",
"nombre": "Camiseta Alternativa - 2023",
"categoria": "Hombre Fútbol",
"precio": 9000,
"disponible": true,
"imgURL": "./recursos/img/productos/futbol-alternativa2023.jpg"
}
]
Project's file structure. I run the webpage by opening merchandising.html (no server running)