2017-10-14 7 views
0

私はAngular JSアプリケーションにWcf Serviceを使用しています。私はユーザー名とパスワードを提供してユーザーログインシステムを作成しています。しかし、これまでに入力したユーザー名やパスワードは、常にユーザー名とパスワードを表示するメッセージが正しいものです。ユーザー名とパスワードが正しければ、角度のjsアプリケーションにメッセージを表示する必要があります。そうでなければ、ユーザー名とパスワードは正しくありませんが、なぜifステートメントでいつもヒットしているのか分かりません。角型JSアプリケーションは、文が決して他の文にならない場合はヒットします

ここに私のスクリプトコードです。ここで

///// <reference path="../angular.min.js" /> 



var app = angular.module("WebClientModule", []) 

    .controller('Web_Client_Controller', ["$scope", 'myService', function ($scope, myService) { 

     $scope.OperType = 1; 

     //1 Mean New Entry 

     //To Clear all input controls. 
     function ClearModels() { 
      $scope.OperType = 1; 
      $scope.Username = ""; 
      $scope.Password = ""; 

     } 
     $scope.login = function() { 
      var User = { 
       Username: $scope.Username, 
       Password: $scope.Password, 
      }; 
      myService.AuthenticateUser(User).then(function (pl) { 

       if (pl.data) { 
        $scope.msg = "Password or username is correct !";//Always hit on this line 
       } 
       else { 
        $scope.msg = "Password Incorrect !"; 
        console.log("Some error Occured" + err); 
       } 
       }, function (err) { 
       $scope.msg = "Password or username is Incorrect !"; 
       console.log("Some error Occured" + err); 
      }); 
     }; 



    }]); 



app.service("myService", function ($http) { 



    this.AuthenticateUser = function (User) { 
     return $http.post("http://localhost:52098/HalifaxIISService.svc/AuthenticateUser", JSON.stringify(User)); 
    } 
}) 

私のHTMLコードは、ここで

@{ 
    Layout = null; 
} 

<!DOCTYPE html> 

<html ng-app="WebClientModule"> 
<head> 
    <meta name="viewport" content="width=device-width" /> 
    <title>Index</title> 
    <script src="~/Scripts/angular.min.js"></script> 

    <script src="~/RegistrationScript/LoginScript.js"></script> 
</head> 
<body> 

    <table id="tblContainer" data-ng-controller="Web_Client_Controller"> 
     <tr> 
      <td></td> 
     </tr> 
     <tr> 
      <td> 
       <div style="color: red;">{{msg}}</div> 
       <table style="border: solid 4px Red; padding: 2px;"> 

        <tr> 
         <td></td> 
         <td> 
          <span>Username</span> 
         </td> 
         <td> 
          <input type="text" id="username" data-ng-model="Username" required="" /> 
         </td> 
        </tr> 
        <tr> 
         <td></td> 
         <td> 
          <span>Password</span> 
         </td> 
         <td> 
          <input type="password" id="password" required data-ng-model="Password" require="" /> 
         </td> 
        </tr> 
        <tr> 
         <td></td> 
         <td></td> 
         <td> 
          <input type="button" id="Login" value="Login" data-ng-click="login()" /> 
         </td> 
        </tr> 
       </table> 
      </td> 
     </tr> 
    </table> 
</body> 
</html> 
<script src="~/RegistrationScript/LoginScript.js"></script> 

はあなたのHTTP POSTリクエストに対するレスポンスを掲載場合には役立つだろうアウト..

click here to see the result

+0

しようとした場合(pl.data =「」!) –

+0

申し訳ありません空の入力との声明 – Mohammad

+0

はそのヒットelse文をfileld場合、まだ打つ - .. $ scope.msg – Mohammad

答えて

1

then()ブロックでは、まず以下のコード console.log(pl.data)を使用してpl.dataの値をチェックしてみてください。 これにより、WCFサービスから返される値を知ることができます。 サービスがTrueを常に返す場合、Angularでは常にif()文が実行されます。 Ex。

myService.AuthenticateUser(User).then(function (pl) { 
console.log(pl.data) 
if(pl.data){ 
    $scope.msg = "Password or username is correct !" 
} 
else{ 
... 
} 
1

を置く..ですです。私は間違ったログイン情報があっても有効なhttpリクエストを作成するので、ログイン情報が正しくないことを示す可能性のあるデータ(pl.data)を持つ有効なhttp応答を得ることができます。

pl.dataがnullまたは未定義の場合は、else文だけを実行します。 JavaScriptの条件を調べ、オブジェクトとの関係を調べるオブジェクトが存在する場合(nullまたはundefinedでない場合)、trueとして処理されます。

+0

をアプリケーションの応答が常に表示されるエラーメッセージ= "パスワードまたはユーザー名が正しいです!"; ..間違ったユーザー名またはパスワードを投稿した場合は問題ありません – Mohammad

+0

出力の結果を追加しました – Mohammad

+0

はい空の入力ファイルIDでヒットelse文 – Mohammad

関連する問題