在 POST 请求后表示不呈现页面
Express not rendering page after a POST request
我的目标是让 onclick 函数发送 post 请求以从页面中删除 HTML li。 post 请求使用 mongoose 函数删除数据,然后我希望重新加载页面,以便用户看到没有他们删除的项目的新页面。删除工作正常,但我必须刷新页面才能看到它被删除。没有任何错误消息。
garage.jade
doctype html
html(lang='en')
head
script
include ../Scripts/garage_scripts.js
body
h1 Welcome to The Garage
h2= title
ul
- for (let i=0; i<list_o_cars.length; i++)
li(onclick='delete_car(this)', id="cars")= list_o_cars[i]
garage_scripts.js
function delete_car(target) {
const data = {
car: target.innerHTML,
};
const url = "http://localhost:3000/car_module/delete";
let request_param = {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify(data),
};
fetch(url, request_param);
}
controller.js
exports.delete = function (req, res, next) {
res.send("hi")
[enter image description here][1]//this would be a render but I am just testing to see if it will reload or redirect to a new page
这是请求预览的图片
也许只需在 fetch() 之后添加 window.location.reload();
。
有人比我解释得更好,HERE
由于获取 returns 承诺,您可以使用 .then() 解决它并重新加载页面。
fetch(url, request_param).then(function(res) {
window.location.reload();
}).catch(function(error) {
console.log(error);
});
我的目标是让 onclick 函数发送 post 请求以从页面中删除 HTML li。 post 请求使用 mongoose 函数删除数据,然后我希望重新加载页面,以便用户看到没有他们删除的项目的新页面。删除工作正常,但我必须刷新页面才能看到它被删除。没有任何错误消息。
garage.jade
doctype html
html(lang='en')
head
script
include ../Scripts/garage_scripts.js
body
h1 Welcome to The Garage
h2= title
ul
- for (let i=0; i<list_o_cars.length; i++)
li(onclick='delete_car(this)', id="cars")= list_o_cars[i]
garage_scripts.js
function delete_car(target) {
const data = {
car: target.innerHTML,
};
const url = "http://localhost:3000/car_module/delete";
let request_param = {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify(data),
};
fetch(url, request_param);
}
controller.js
exports.delete = function (req, res, next) {
res.send("hi")
[enter image description here][1]//this would be a render but I am just testing to see if it will reload or redirect to a new page
这是请求预览的图片
也许只需在 fetch() 之后添加 window.location.reload();
。
有人比我解释得更好,HERE
由于获取 returns 承诺,您可以使用 .then() 解决它并重新加载页面。
fetch(url, request_param).then(function(res) {
window.location.reload();
}).catch(function(error) {
console.log(error);
});