【发布时间】:2021-12-04 19:32:28
【问题描述】:
我正在尝试从我的 api 获取一些本地数据并使用按钮将其发布到浏览器控制台上,但是在按钮的功能中它不起作用但在 onclick 功能之外它工作得很好,我什至尝试了获取的返回语句。我正在尝试这样做,所以我可以在 JS 生成的 html 表上显示所有数据。不管怎样,这里是 html 代码,后面是 JS 代码:
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代码:
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);
【问题讨论】:
-
你不需要bodyParser,你可以使用
app.use(express.json()) -
如果您将正确的标签添加到您的问题中以表示“快递”或其他任何内容,您可能会得到更多回复。
-
另外,您似乎在请求 /users 而不是 /Users。
-
用户和用户是一样的
标签: javascript html express