【发布时间】:2018-10-07 20:37:47
【问题描述】:
我将数据库连接到节点并尝试创建一个 HTML 页面来搜索数据库。我宁愿不使用 EJS。我想我必须在 HTML AJAX 中使用 POST 请求并将其与节点中的 POST 请求连接起来。
这是我的想法:
app.post("/cities/:city", function(req, res) {
db.hospitalinfo.findAll({
where: { city: req.params.city }
}).then(function (result) {
res.json(result);
console.log("res--->"+result);
console.log("req--->"+req.params.city);
});
});
这是 HTML:
<form id="author-form" method="POST">
<select id ="dropDownId">
<option value="Opp" >Opp</option>
<option value="Boaz">Boaz</option>
</select>
<input class="SubmitButton" type="submit" id="click" style="font-size:20px;" />
</form>
现在我遇到了困难。我需要从 select 语句中获取值:
var nameInput = $("#dropDownId :selected");
我不知道如何将 nameInput 实际发送到 URL,因此我的 post 语句将起作用。我可能不完全理解这些路线是如何工作的。这是我自己的第一个项目。我想获取 nameInput,通过 AJAX 将其发送到服务器,然后用它搜索我的数据库。现在它返回一个空对象。感谢您的帮助。
【问题讨论】:
-
这是一个没有 ExpressJS 的 API 路由示例:stackoverflow.com/questions/45864677/…
-
我正在使用 express 我只是不想使用像 EJS 这样的后端模板语言,如果我可以避免的话,因为我无法使用它访问 DOM。也许有一种我不知道的方法。
标签: html mysql node.js ajax select