javascript - How to re render a .ejs page with new data after ajax request
问题描述
I need to re render my homepage.ejs with new data on the server side after an ajax request. I know you can (somehow) re render the elements in the ajax callback but I was wondering if I could just re render my homapage.ejs since I have if statements that look for vacations, and current_user variables to set the right partialview.ejs
my ajax method:
function user_login(email, password) {
var data = {};
data.email_login = email;
data.password_login = password;
var message_header = $("#message_header");
$.ajax({
url: "/sign_in",
method: 'POST',
data: data
}).done(function (res) {
if (!res.success) {
message_header.text("Incorrect email of password. Please try again.").css("color", "red");
$("#username_textfield").val("");
$("#password_textfield").val("");
}
if (res.success) {
console.log("user logged");
}
});
my server side (inside of a query callback)
get_all_vacations(function (error, vacations) {
if (error) {
console.log("error when getting vacations " + error);
response.json({ success: false });
} else {
request.session.vacations = vacations;
response.render('the_vacation.ejs', { vacations : vacations, current_user : new_current_user }, function() {
response.json({success : true});
console.log("in render method");
});
}
});
i tried this but it does not work.
response.render('the_vacation.ejs', { vacations: vacations, current_user: new_current_user });
解决方案
我认为根据您的代码编写方式,最简单的方法是window.location.reload();
在登录成功时使用。
function user_login(email, password) {
var data = {};
data.email_login = email;
data.password_login = password;
var message_header = $("#message_header");
$.ajax({
url: "/sign_in",
method: 'POST',
data: data
}).done(function (res) {
if (!res.success) {
message_header.text("Incorrect email of password. Please try again.").css("color", "red");
$("#username_textfield").val("");
$("#password_textfield").val("");
}
if (res.success) {
console.log("user logged");
window.location.reload(); // <--- Cause the browser to reload the EJS
}
});
推荐阅读
- ruby-on-rails - Rails:HAML 上的 button_tag 与常规 %button
- python - 在 cython 中调用 np.sqrt
- properties - 具有属性的自定义组件
- c# - 关于C#实体框架理解的问题
- bash - Bash Multiple -execdir or -exec statements NOT WORKING 当分成多行以进行正确的代码格式化时
- javascript - 如何在 Visual Studio 的文本文件中读取包含 JSON 的 url,然后在另一个 txt 文件中加载 JSON 写入?
- html - 安排用html css脚本等编写的代码的方法(或记事本++的热键)是什么
- apache - Apache Vhosts HTTP 和 HTTPS(两者)重定向到另一个域
- html - Nginx 死机但 pid 文件存在
- java - 我正在重现约翰康威的生命游戏。然而,我对游戏规则的检查并没有正确地检测到周围的生命。为什么是这样?