Rupesh Yadav Rupesh Yadav - 3 years ago 200
Ajax Question

How to successfully send keys from nodejs to Ajax

I want to send key

(success and failure )
from nodejs like

res.send(JSON.stringify({success : "successfully registered"}));


or

res.send(JSON.stringify({failure : "user already exist"}));


to ajax

I want that if key
failure
occur then show this
user already exist
and if success occur it redirect to next page

My code is this But It not doing according to me

Please solve my problem

$.ajax(
{
url: "/register",
method: 'POST',
data: JSON.stringify(data),
contentType: 'application/json',
success: function (result) {
if(result === 'success') {
window.location= '/profile';
}
else
{
Materialize.toast(result,2000);
}},
error: function (err) {
console.log(err);
}})
});

Answer Source

Try

res.send({status: "success", message : "successfully registered"});

Same for failure. And to get this

if(result.status == "success") {
    //success code
} else {
    //failure code
}

Also you can get the message for using result.message

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download