satheesh prabhakaran satheesh prabhakaran - 2 months ago 12
HTML Question

Not getting response from post method in nodeJS to html page

This is my nodeJS code:

app.post('/signup', function (req, res) {
res.status('500').send('showAlert');
});


This is my Html post method call

$.post("/signup", userData, function(error){
if(error.responseText == 'showAlert')
{
alert("User already exists");
}
});


Why am I not getting any alert?

Answer

Status code 500 is indicator of Internal Server Error which is handled in error handler of ajax.

jQuery.post( url [, data ] [, success ] [, dataType ] )

Refer this example:

var jqxhr = $.post( "example.php", function() {
  alert( "success" );
})
  .done(function() {
    alert( "second success" );
  })
  .fail(function() {
    alert( "error" );
  })
  .always(function() {
    alert( "finished" );
});

Updated code:

$.post("/signup", userData, function(data) {
  console.log(data);
}).fail(function(error) {
  if (error.responseText == 'showAlert') {
    alert("User already exists");
  }
});
Comments