使用 post (NodeJS) 时页面未通过 Express 渲染
我正在使用 Express v.2.4.6 (Node.js - v.0.6.2)。 我试图在调用 POST(而不是 GET)后呈现(或重定向到)新页面。当 GET 被调用时我能够渲染/重定向。但是,当在 Express 中调用 POST 时,我似乎无法呈现页面。我不确定这是否可能,尽管 Express 网站上的指南确实提到了一个示例,您可以在调用 POST 后进行重定向。 相关代码如下(客户端以 JSON 格式发送表单)。我可以在 Node.j 中成功解析 JSON 消息。
示例路由:
app.post('/signup', function(req, res){
res.redirect('index');
//res.render('index');
});
不会引发异常,但索引页面不会呈现或重定向。 任何反馈将不胜感激。
I am using Express v.2.4.6 (Node.js - v.0.6.2).
I am trying to render (or redirect to) a new page once POST is called (as opposed to GET). I am able to render/redirect when GET is called. However, I cannot seem to render a page when POST is called in Express. I am not sure if this is even possible though the guide on the Express site does mention an example where you can redirect once POST is called.
The relevant code is given below (client is sending the form in JSON). I can parse through the JSON message successfully in Node.j.
Sample route:
app.post('/signup', function(req, res){
res.redirect('index');
//res.render('index');
});
No exceptions are thrown, but the index page does not get rendered nor redirected.
Any feedback will be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许您从客户端从
$.ajax
调用它如果您从服务器端调用它,效果很好
请参考这个问题。
Express.js 不会在后期操作中呈现
Maybe you call it from
$.ajax
from client-sideIt works well if you call it from server-side
Please refer to this question.
Express.js Won't Render in Post Action
res.redirect('/')
很可能是您想要的,但您可以在任何路由中渲染,重定向只是大多数人使用的约定res.redirect('/')
is most likely what you wanted there, but you can render in any route, redirecting is just a convention that most people use