[英]Node.js POST request without redirect
Originally, my routes would redirect the user to the page they submitted the request. 最初,我的路线会将用户重定向到他们提交请求的页面。 However, this sends them back to the top of the page.
但是,这会将它们发送回页面顶部。
I implemented AJAX to submit the request, but what do I write in the route? 我实现了AJAX来提交请求,但是我在路由中写了什么? I was originally using res.redirect, but switched wherever it said
res.redirect
to return
, thinking that that would work, however my page just gets stuck in a process and after a minute or so, the page errors. 我最初使用的是res.redirect,但是切换到说
res.redirect
return
,以为那可以工作,但是我的页面只是卡在一个进程中, res.redirect
一分钟后,页面错误。
What is the correct way to process a request and then just stop? 处理请求然后停止的正确方法是什么?
How come return
doesn't work? 为什么
return
不起作用?
Use them together like this: 像这样一起使用它们:
function(req, res, next) {
if (/*<case when you need redirect>*/) {
return res.redirect('/path/to/redirect');
} else if (/*<case when you need simple response>*/) {
return res.send();
}
// some other code here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.