没有得到从postJ方法在nodeJS到HTML页面的响应

这是我的nodeJS代码:

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

这是我的Html post方法调用

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

为什么我没有得到任何警报?

状态码500是在ajax error处理程序中处理的Internal Server Error指示符。

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

参考这个例子:

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

更新的代码:

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

这是一个参考什么状态代码的意思。

错误4xx,5xx

4xx代码用于客户端似乎有错误的情况,而5xx代码用于服务器知道服务器已经出错的情况。 一般情况下不可能区分这些情况,所以区别只是信息性的。

因此,您应该单独处理错误响应。

 $.post("/signup", userData, function(data) { console.log(data); // executed if status code starts from 2XX }).error(function(error) { // executed if error ie status code 4xx or 5xx if (error.responseText == 'showAlert') { alert("User already exists"); } });