V
V
Vyacheslav Knyazev2018-11-20 19:42:44
Node.js
Vyacheslav Knyazev, 2018-11-20 19:42:44

Node.js express: res.redirect(path) sends html to the client. Why?

via AJAX (XMLHttpRequest. POST request) I get the login form.
on the server (node.js express) I do a check.
if the check does not pass, I send a refusal to the client (a window that the data is not correct) - everything is fine, it works.
when the check passes, when:
res.redirect(path_to_redirect)
html of the redirect page arrives on the client, but not the redirect itself.
what's wrong? where to dig?
made a crutch - send:
res.redirect(200, path_to_redirect);
and on the client I cut off from the answer 'OK. Redirecting to ', and assign everything else to href
and then window.location.href = href;
And of course there is a redirect.
but this is of course a harsh perversion, and what to do with Bathert?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
Tendor, 2018-11-20
@Tendor

send a post request

V
Vyacheslav Knyazev, 2018-11-21
@slavak-ru

for some reason, the answer of Abcdefgk is not displayed :
"Yes, you don't need to do res.redirect at all.
On the client, you need to cancel the "default action" (do not reload the page - for sure, the button on the form has type='submit').
In the response to the request, send the path "redirect" - res.send('/bla-bla')
On the client do window.location = xhr.responseText"
wanted to thank him.
did as suggested by Abcdefgk , so it works.
and as he correctly guessed, on the page there is a form, a button (submit). client js, canceling the default action, collects and sends data.
the server accepts and if everything is ok sends a redirect. ( instead of res.redirect(path) did res.send(path) )
but the question remains - why doesn't the server redirect to POST work?
those. return res.redirect(path)

is required

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question