I'm current in /id/1234
page, and click a POST /add
button, after that I want to reload /id/1234
in Experss.js:res.redirect(**How to get /id/1234 dynamicly, since I will be in /id/1235, /id/1236 page do a same POST /add action?**)
Thanks.
In JavaScript, you refresh the page using document. location. reload() . You can add the true keyword to force the reloaded page to come from the server (instead of cache).
set('view engine', 'ejs'); app. use(bodyParser. urlencoded({ extended: false })); var requests; app. get('/', function(req, res) { res.
When Nodemon restarts the ExpressJS server on changes, Livereload recreates the server and sends to the browser a refresh command when connected liveReloadServer. refresh("/"); . app.
The error "Error: Can't set headers after they are sent." means that you're already in the Body or Finished state, but some function tried to set a header or statusCode. When you see this error, try to look for anything that tries to send a header after some of the body has already been written.
I'd just like to add that in the version 4.x of Express you can use
res.redirect('back');
to automatically redirect back to the page the request came from. This is the equivilant of
res.redirect(req.get('referer'));
that is mentioned in Peter Lyons answer
See also: http://expressjs.com/api.html#res.redirect
To give you the answer you probably want, the browser will send a header called [Referer][1]
which will have the URL of the /id/1234
page, so do:
res.redirect(req.get('referer'));
However, your URL path design is probably poor if you need to do this. Better options might be submitting the form via AJAX without changing the URL or including the 1234
id in the form body and using that value from the POST request to generate the correct URL for the corresponding redirect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With