【发布时间】:2014-05-26 10:41:03
【问题描述】:
我有一个非常基本的 HTML 表单,我正在使用 jQuery 的 AJAX 发布功能来获取一些信息,具体取决于数据库插入是成功还是失败。
我的代码目前正在执行以下操作,我仅在通过 AJAX 发布时将 ajax 发布变量设置为 true 以区分来源。
app.post("/mypath", function (req, res) {
var post = req.body;
if (post.ajax) {
console.log(post.ajax, "posting as ajax");
}
if (post.ajax) {
res.json({data: "Woah! You posted as ajax."});
}
else {
// If posting via HTML form, send the user where they need to go
res.redirect("someview");
}
});
我真正担心的是,我不希望最终用户看到带有纯文本 JSON 的页面,但是当我支持 JavaScript 时,我想要一些 JSON 数据,以便通知用户成功/失败。
有什么办法可以让这个更干净吗?我有 3 个地方必须在那个应用程序路径中进行条件渲染。
我正在使用 Express 3.x
【问题讨论】:
标签: javascript jquery ajax json express