节点 GET 请求不会显示在 html 页面上,仅加载 - 不会出现 return 错误
node GET request won't show up on html page, only loads - doesnt return error
当我从服务器发送 route.get 请求时,它成功响应 JSON 数据并显示在 localhost:/view/product 页面上,但是,我希望它处理我的客户端html 页面并将数据库信息显示到 table 那里。它不是那样做的。
我的目标是获取此数据并将其显示到 localhost:3000/view/product 路线上的简单 html table。
如有任何建议,我们将不胜感激,提前致谢。
我的htmltable尝试:
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="app.css">
<link rel="stylesheet"
href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.no-icons.min.css">
<div>
<header>
<h2><a href="/"><i></i> VIEW PRODUCTS </a></h2>
</header>
</div>
</head>
<body>
<div id="table">
<table align='center' cellspacing=2 cellpadding=5 id="productTable" border=1>
<thead>
<th> Product Name </th>
<th> Quantity </th>
<th> Price </th>
</thead>
<tbody id="productTable"></tbody>
</table>
</div>
</body>
</html>
<script>
$.get('/view/product', {}, function(results) {
let res = '';
results.forEach(data => {
res += `<tr><td>${data.productname}</td><td>${data.quantity}</td><td>${data.price}</td></tr>`
})
$('#place-here').html(res)
});
</script>
我的获取请求:
route.get("/view/product", async function (req, res) {
try {
const results = await db.query("SELECT * FROM product ORDER BY createdAt DESC;",
);
// console.log(results);
// res.status(200).json({
// status: "success",
// results: results.rows.length,
// data: {
// orders: results.rows,
// },
// });
} catch (err) {
console.log(err);
}
});
正在我的 index.js 文件中加载 html 页面:
app.get('/view/product',function(req,res){
res.sendFile('view.html', { root: __dirname });
});
您不能为您的页面加载和 Ajax 调用以获取 JSON 使用相同的路径,因为它们都是 GET 路由,所以只有第一个在您的 Express 服务器上注册将永远有机会看到传入的请求。
我建议您将 Ajax 调用更改为 /api/products
之类的内容,然后在您的服务器上更改 returns JSON 路由的相应定义。
当我从服务器发送 route.get 请求时,它成功响应 JSON 数据并显示在 localhost:/view/product 页面上,但是,我希望它处理我的客户端html 页面并将数据库信息显示到 table 那里。它不是那样做的。
我的目标是获取此数据并将其显示到 localhost:3000/view/product 路线上的简单 html table。
如有任何建议,我们将不胜感激,提前致谢。
我的htmltable尝试:
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="app.css">
<link rel="stylesheet"
href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.no-icons.min.css">
<div>
<header>
<h2><a href="/"><i></i> VIEW PRODUCTS </a></h2>
</header>
</div>
</head>
<body>
<div id="table">
<table align='center' cellspacing=2 cellpadding=5 id="productTable" border=1>
<thead>
<th> Product Name </th>
<th> Quantity </th>
<th> Price </th>
</thead>
<tbody id="productTable"></tbody>
</table>
</div>
</body>
</html>
<script>
$.get('/view/product', {}, function(results) {
let res = '';
results.forEach(data => {
res += `<tr><td>${data.productname}</td><td>${data.quantity}</td><td>${data.price}</td></tr>`
})
$('#place-here').html(res)
});
</script>
我的获取请求:
route.get("/view/product", async function (req, res) {
try {
const results = await db.query("SELECT * FROM product ORDER BY createdAt DESC;",
);
// console.log(results);
// res.status(200).json({
// status: "success",
// results: results.rows.length,
// data: {
// orders: results.rows,
// },
// });
} catch (err) {
console.log(err);
}
});
正在我的 index.js 文件中加载 html 页面:
app.get('/view/product',function(req,res){
res.sendFile('view.html', { root: __dirname });
});
您不能为您的页面加载和 Ajax 调用以获取 JSON 使用相同的路径,因为它们都是 GET 路由,所以只有第一个在您的 Express 服务器上注册将永远有机会看到传入的请求。
我建议您将 Ajax 调用更改为 /api/products
之类的内容,然后在您的服务器上更改 returns JSON 路由的相应定义。