Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

290
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!