Jquery node.js+;mysql+;ejs在数据插入后更新屏幕信息而不重新加载页面
我尝试在插入后更新我的任务列表,当前当我按下按钮时,在我的ejs页面中我提交了一个post请求,而在我的控制器中我有app.post(“/mypage”),在这里我使用connection.query()进行插入,然后我使用res.redirect()重新加载页面,因此我的app.get(“/mypage”)重新加载我的所有数据并显示在我的页面上,有没有不重新加载数据的方法?我曾考虑使用jqueryajax,但找不到足够的信息源 要添加的ejs模式Jquery node.js+;mysql+;ejs在数据插入后更新屏幕信息而不重新加载页面,jquery,mysql,node.js,ajax,ejs,Jquery,Mysql,Node.js,Ajax,Ejs,我尝试在插入后更新我的任务列表,当前当我按下按钮时,在我的ejs页面中我提交了一个post请求,而在我的控制器中我有app.post(“/mypage”),在这里我使用connection.query()进行插入,然后我使用res.redirect()重新加载页面,因此我的app.get(“/mypage”)重新加载我的所有数据并显示在我的页面上,有没有不重新加载数据的方法?我曾考虑使用jqueryajax,但找不到足够的信息源 要添加的ejs模式 <div class="modal" i
<div class="modal" id="addTask">
<div class="modal-content">
<form action="/studentDashTaskAdd/<%=uID%>/<%=pID%>" method="post">
<div class="row">
<div class="input-field col s12">
<input
id="task_title"
name="taskTitle"
type="text"
class="validate"
/>
<label for="task_title">Title</label>
</div>
</div>
<div class="row">
<div class="input-field col s12">
<textarea
id="textarea1"
name="taskDescription"
class="materialize-textarea"
></textarea>
<label for="textarea1">Description</label>
</div>
</div>
<div class="row">
<div class="input-field col s12">
<input
type="text"
id="taskDeadine"
name="taskDeadline"
class="datepicker"
/>
<label for="taskDeadine">Deadline</label>
</div>
</div>
<div class="row">
<div class="input-field col s12">
<select id="priority" name="taskPriority">
<option value="1">Low</option>
<option value="2">medium</option>
<option value="3">High</option>
</select>
<label for="priority">Priority</label>
</div>
</div>
<button type="submit" name="submidTask" class="btn blue right">
Submit <i class="material-icons right">send</i>
</button>
</form>
</div>
<div class="modal-footer"></div>
</div>
我最近开始使用您的工具组合,因此我不确定是否为您提供了最佳方法,但我会使用jquery: JQuery
$.post(
"studentDashTaskAdd",
{yourValues},
(data, status) =>
{
switch(status){
case "error":
// Manage your issues here
return;
case "timeout":
// Manage your issues here
return;
case "parsererror":
// Manage your issues here
return;
}
// then either you send back the full list and treat it here or just append what you just sent to your server
},
"json"
})
NodeJS
app.post(
"/studentDashTaskAdd",(req,res) = >{
const query = " your query"; //req.body still works in this case for your params
connection.query(query,(err,results) => {
if(err){
res.status(400).send({message:err});
}
res.send("your data as json or anything to tell your ajax Ok It's done");
})
});
如果有人纠正我的错误,我会很高兴。如果我错了,我还在学习:p
注意:你也可以在你的按钮上添加一个id,比如id=“addTask”,然后使用“{code>$”(“#addTask”)。点击(()=>{///我在上面做了什么})”注意:你也可以在你的按钮上添加一个id,比如id=“addTask”,然后使用$(“{addTask”)。点击(()=>{///我在上面做了什么})”这就是我正在做的事情,但是在哪里编写jquery呢?我目前正在ejs文件中执行此操作,因为如果我尝试将代码放在静态文件中,它将无法识别数据并抛出错误。我猜您是在尝试使用发送到ejs页面但未在任何位置打印的数据,然后将其打印在脚本标记中?一种解决方法是使用隐藏的html标记,其中包含数据或值
在jquery中,您可以像这样访问它$(“#test”).attr(“您的值的数据名”)
app.post(
"/studentDashTaskAdd",(req,res) = >{
const query = " your query"; //req.body still works in this case for your params
connection.query(query,(err,results) => {
if(err){
res.status(400).send({message:err});
}
res.send("your data as json or anything to tell your ajax Ok It's done");
})
});