2017-11-07 1 views
0

trueまたはfalseのwebmethod応答を解析するif文を含めることを試みています。私はちょうど、応答が真である場合に投稿が成功したこと、または応答が偽であれば投稿が成功しなかったことをユーザーに警告する必要があります。WebMethodの応答と使用If文応答に基づいてユーザーに警告する

私はxhttp.responseTextを使用して応答を得ることができますが、私は私のjavascriptの内部に以下のif文の中にそれを構築する方法を見つけ出すことはできません。

//JavaScript that Posts to WebMethod  
<script> 
    function createNewComment() { 

     var xhttp = new XMLHttpRequest(); 
     var url = "http://localhost:57766/PALWebService.asmx/insertComment" 
     var a = document.getElementsByName("existingguid")[0].value; 
     var b = document.getElementsByName("newcomment")[0].value; 
     var c = 'existingguid=' + a + '&newcomment=' + b; 

     xhttp.open("POST", url, true); 

     xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 

     xhttp.onreadystatechange = function() { 
      if (this.readyState == 4 && this.status == 200) { 

      } 
     }; 

     xhttp.send(c); 
    } 
</script> 

答えて

0

が、私はそれを考え出しました。 readyStateが4でステータスが200であることを確認した後、XMLHttpRequestからresponseTextをチェックするために別のif文を入れ子にして、別の関数を呼び出したのは真だったのですが、falseの場合はwebmethodに失敗しました。それは完璧ではないかもしれませんが、私が必要とするもののために働きます。

xhttp.onreadystatechange = function() { 
      if (this.readyState == 4 && this.status == 200) { 
       if (xhttp.responseText = true) { 
        addComment(b, today, userName); 
       } 
       else { 
        document.getElementsByName("newcomment")[0].value = ''; 
        $("#commentLabel").html("Your comment was not saved in the database. Please try again or contact system admin."); 
       } 
      } 
     }; 
関連する問題