2016-09-29 5 views
1

にnodeJSでPOSTメソッドからの応答を取得していないこれは私のnodeJSコード:は、htmlページ

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"); 
     } 
    }); 

なぜ私は任意のアラートを取得しておりませんでしょうか?

答えて

2

ステータスコード500ajaxerrorハンドラで処理され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"); 
    } 
}); 
0

Hereは、コードの意味状態への参照です。

エラーの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 i.e status code 4xx or 5xx 
if (error.responseText == 'showAlert') { 
    alert("User already exists"); 
} 
});