如何解决如何从Express获取记录集并打印到html容器div
我正在尝试使用Node Express和sql Server创建库存管理器。当我单击按钮时,它当前会打开一个新窗口并在其中显示记录集。我希望记录集显示在PersonContainer div的主窗口中。最初我使用的是app.post,但在与几个人交谈之后,我现在正在使用app.get来实现此特定功能。我也相信我的脚本指向了正确的方向,但是我认为并不是所有代码都可以执行。
还要注意,该项目的结构是允许用户向数据库中添加某些捕捞产品。目前,该项目的插入部分工作正常,我可以轻松地从该项目的前端将产品添加到数据库中。我只希望我的记录集显示个人在容器中而不是在新窗口中拥有的产品。目前,人员表中有一些测试人员用于测试。
请帮助!
index.html PersonContainer
<div id='PersonContainer' class="DBcontainer">
<form action="/person" method="GET">
<button onclick="displayPerson()">Click me</button>
</form>
</div>
index.html PersonContainer脚本
<script>
function displayPerson() {
console.log('click working');
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
var person = xhttp.responseText;
var element = document.getElementById("PersonContainer");
person.forEach(function (item,index) {
element.innerHTML += '<li><p>' + item.name + '</p></li>'
});
}
};
xhttp.open("GET","/person",true);
xhttp.send();
}
</script>
Server.js
app.get('/person',function (req,res) {
var conn = new sql.Connection(config);
var requ = new sql.Request(conn);
conn.connect(function (err) {
if (err) {
console.log(err);
return;
}
var sql = "SELECT * FROM person";
requ.query(sql,function (err,recordset) {
if (err) {
console.log(err)
} else {
res.send(recordset);
}
});
conn.close();
});
});
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。