admin管理员组文章数量:1327690
I have a html form for my node app and if it gets submitted it shows /code and the text but nothing more and i can't redirect to my Mainpage. How can i redirect after it gets submitted with a Timer to another page? I tried to do it in app.post and clientsided but its not working
<form action="/code" method="post" >
<fieldset>
<input type="text" name="code" id="code"
style="text-align: center"
placeholder="Enter Code" /> <br> <br> <input
type="submit" class="btn btn-success" name="submit" id="submit"
value=" authentifizieren " />
</fieldset>
</form>
App.js
app.post('/code', function(req, res) {
var code = req.param("code");
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('Code: '+code+');
res.end();
});
I just want to redirect from app.post /code back to my mainpage or any other website.
I have a html form for my node app and if it gets submitted it shows /code and the text but nothing more and i can't redirect to my Mainpage. How can i redirect after it gets submitted with a Timer to another page? I tried to do it in app.post and clientsided but its not working
<form action="/code" method="post" >
<fieldset>
<input type="text" name="code" id="code"
style="text-align: center"
placeholder="Enter Code" /> <br> <br> <input
type="submit" class="btn btn-success" name="submit" id="submit"
value=" authentifizieren " />
</fieldset>
</form>
App.js
app.post('/code', function(req, res) {
var code = req.param("code");
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('Code: '+code+');
res.end();
});
I just want to redirect from app.post /code back to my mainpage or any other website.
Share asked Sep 17, 2014 at 16:01 bastelbastel 511 silver badge9 bronze badges 2- Can you handle the redirect on the client side (through an ajax success method) and just return a res.send(200) from the post call on the server? Seems like that's the way to go. – Christopher Marshall Commented Sep 17, 2014 at 16:10
- could you redirect from server? why not? – sites Commented Sep 17, 2014 at 16:16
3 Answers
Reset to default 5Redirects in Node JS:
res.writeHead(302, {
'Location': 'http://example./'
});
res.end();
If you want to do it client-side, in Node JS:
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('Code: '+code);
res.write('<script>setTimeout(function () { window.location.href = "http://example./"; }, 5000);</script>');
res.end();
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('Code: '+code);
res.write('<script>setTimeout(function () { window.location.href = "http://example./"; }, 5000);</script>');
res.end();
Two types of redirection can be done in pure node js
1)Within the same site
res.writeHead(301, {"Location": "/blog/thank-you"});
return res.end();
2)To an External website
res.redirect("https://stackoverflow./");
本文标签: javascriptNode JS redirection after submiting a formStack Overflow
版权声明:本文标题:javascript - Node JS redirection after submiting a form - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742220155a2435302.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论