gpt4 book ai didi

javascript - 如何在 DELETE 路由中发送消息 - express.js

转载 作者:搜寻专家 更新时间:2023-11-01 00:36:55 25 4
gpt4 key购买 nike

我想在删除用户后显示消息,但我不知道该怎么做。我尝试创建 req.session 属性然后使用它们,但它们在 GET 路由中不可用。您知道如何修复此代码吗?

router.get("/", mid.isExpired, mid.isLoggedIn, mid.isAdmin, (req, res) => {
let currentMessage = req.session.message;
let currentState = req.session.state;
req.session.message = undefined;
req.session.state = undefined;
console.log(currentState, currentMessage); //undefined
user.getAll()
.then(result => {
res.render("users", {
name: req.user,
users: result,
msg: currentMessage,
state: currentState
})
})
});


// delete route

router.delete("/delete/:id", mid.isExpired, mid.isLoggedIn, mid.isAdmin, (req, res) => {
user.del(req.params.id)
.then(() => {
req.session.message = "Some message!"
req.session.state = true;
})
});

// jquery

function ajaxDelete(ev, url) {
ev.preventDefault();
$.ajax({
url: url,
type: "DELETE"
});
}

delBtn.click(function(e) {
var user = $(this).data("user");
ajaxDelete(e, "/users/delete/" + user);
window.location.href = "/users";
})

最佳答案

使用res参数,创建一个名为message的变量

const message= 'MyMessage';

然后

res.json ({message}) // es6 feature

输出

{"message":"myMessage"}

关于javascript - 如何在 DELETE 路由中发送消息 - express.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48396250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com