Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

293
Vistas
Trying to show the data from my local api through an onclick function wont work, what's the solution?

I am trying to fetch some local data from my api and post it on the browsers console using a button however it doesnt work when in the button's function but it works perfectly fine when it is outside the onclick function, i even tried a return statement for the fetch. I am trying to do so, so i can display all the data on an html table generated by JS. Anyways here is the html code followed by the JS code :

function getUsers(){
return fetch('http://localhost:3000/users')
.then(response => response.json())
.then(json => console.log(json))
}
<button type="submit" class="btn btn-outline-light" onclick="getUsers()">Users</button>

js code :

const express = require('express');
const app = express();
var cors = require('cors');
const bodyParser = require('body-parser');
//server stuff end

const xlsx = require('xlsx');
const file = xlsx.readFile('./testing.xlsx');
const sheet = file.Sheets['Sheet1'];
const content = xlsx.utils.sheet_to_json(sheet);
//reading xlsx file from sheet 1

app.use(bodyParser.json());
app.use(cors()); //using cors so we can call it from html file

app.get('/', function (req, res) {
 })


app.get('/Users', function (req, res) {
res.send(content);
})


app.listen(3000);
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Could you please describe what "doesnt work" means? Is the onclick()-Function placed at the right position in code, what does your developer console tells you when clicking the button and have you tried a simple console.log statement in the onclick()-function to evaluate if it is fired at all?

I tried the code (with my own url) and it worked as expected.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda