我使用 Node.js 作为服务器端,并使用 Express 和 Twitter Bootstrap 作为前端。该页面有一个带有表单和提交按钮的对话框;该表单是通过 Jquery Ajax 调用提交的,在 Node.js 服务器响应后不要重新加载页面。我希望执行以下三个步骤:
page.ejs
<table id="tblProgs" class="table table-bordered table-hover table-striped dataTable">
<thead>
<tr>
<th>
Column
</th>
</tr>
</thead>
<tbody>
<% datas.forEach(function(data) { %>
<tr>
<th width="15%">
<%- data._column %>
</th>
</tr>
<% }) %>
</tbody>
</table>
Ajax 内容也在 page.ejs 主体中
$('#formId').submit(function (e){
e.preventDefault();
$.ajax({
type: 'POST',
data: JSON.stringify(data),
cache: false,
contentType: 'application/json',
datatype: "json",
url: '/route/to/nodejs',
success: function (result) {
console.log("Here are the response in json format: " + result);
}
});
});
The url /route/to/nodejs
调用这个函数:
在node.js服务器中调用的函数:
query = "SELECT * FROM table_name";
sequelize.query(query, {
type: sequelize.QueryTypes.SELECT
}).success(function (result) {
var response = {
data : result,
}
res.send(response);
}).catch(function (error) {
res.render('server-error', {
user: user,
session: req.session,
error: error
});
});
我的问题是:如何在成功ajax回调中刷新page.ejs处的表数据而不重新加载页面?
Thanks!