Lalit P
Lalit P

Reputation: 15

How to show error message on same page in node js?

I am using post method to submit form in node js. Now when I submit the form, I want to show the error message from node server if login fails. I am setting the message as res.send("") but it is showing message on another window. So please help me to solve this?

Upvotes: 0

Views: 3230

Answers (2)

mrvautin
mrvautin

Reputation: 300

You could make an Ajax call from your form to your node server API, then depending on the result from your server, show a message.

Jquery example:

$.ajax({
       method: "POST",
       url: "/api"
       data: {login_data_here}
})
.success(function(msg) {
       // redirect to admin area etc
})
.error(function(msg) {
       // show login for with a failure message
});

Node API server would return something like this:

res.writeHead(200, { 'Content-Type': 'application/text' }); 
res.end('Login Success');

Or this on fail:

res.writeHead(400, { 'Content-Type': 'application/text' }); 
res.end('Login Fail');

Upvotes: 1

vodolaz095
vodolaz095

Reputation: 6986

You can use this module for persisted flash messages between page redirects - https://www.npmjs.com/package/connect-flash

Upvotes: 0

Related Questions