I have a static azure web app and an azure function, both running locally. The function is working and showing results in the browser.
The application is a vanilla javascript app. And I have the following code:
The fetch function
async function fetchResources(city) {
try {
const response = await fetch("http://localhost:7071/api/GetResources/Bogotá");
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
console.log(data);
return data;
} catch (error) {
console.error('Error fetching data:', error);
//cityListContainer.innerHTML = `<p>Lo sentimos, no pudimos cargar los datos.</p>`;
}
}
I have a function that loads content according to the path.
else if (path === "/Bogotá") {
const resources = fetchResources("Bogotá");
resources.forEach((resource) => {
const item = document.createElement('div');
item.innerHTML = `
<h3>${resource.name}</h3>
<p>Coordinador: ${resource.coordinator}</p>
<p>Hora: ${resource.time}</p>
<a href="${resource.whatsappGroup}" target="_blank">Grupo de WhatsApp</a>
`;
container.appendChild(item);
});
}
when I go to the web app url: http://127.0.0.1:5500/Bogot%C3%A1, it is succesfully enrouted, and then it enters the fetch function and crashes, without any message in the line "const response = await fetch("http://localhost:7071/api/GetResources/Bogotá");". Or If I don't step into the method, it reaches "resources.forEach((resource)" in the load content function. Once I hit F10 again, it crashes.
the result is:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot GET /Bogot%C3%A1</pre>
</body>
</html>
and the message is:
Bogot%C3%A1:1
GET http://127.0.0.1:5500/Bogot%C3%A1 404 (Not Found)
That response comes from the web app not the function. In the function output I get this.
[2024-11-22T22:19:59.560Z] Executing 'Functions.GetResources' (Reason='This function was programmatically called via the host APIs.', Id=fe5e3e91-c355-4fa5-9253-09b3206f954d)
[2024-11-22T22:19:59.572Z] C# HTTP trigger function processed a request.
[2024-11-22T22:19:59.573Z] Executing OkObjectResult, writing value of type 'Azure.Core.PageableHelpers+FuncPageable`1[[Azure.Data.Tables.TableEntity, Azure.Data.Tables, Version=12.9.1.0, Culture=neutral, PublicKeyToken=92742159e12e44c8]]'.
[2024-11-22T22:20:00.175Z] Executed 'Functions.GetResources' (Succeeded, Id=fe5e3e91-c355-4fa5-9253-09b3206f954d, Duration=615ms)
I cannot figure out what I am missing. I understand the function was run correctly and the response sent, but the web app did not received it.
Update your frontend code URL as shown below:


fetchResourcesis an async function and thus returns a promise, this means thatconst resources = fetchResources("Bogotá")isn't going to work the way you expect it to. You need to await it (const resources = await fetchResources("Bogotá")) or use thethenmethod (fetchResources("Bogotá").then(resources => ...).