Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

292
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda