2017-10-14 43 views
0

我正在使用Wcf服務到Angular JS應用程序。我正在通過提供用戶名和密碼來創建用戶登錄系統。但是,我在輸入中輸入的任何用戶名或密碼都會提示其始終顯示消息用戶名和密碼是否正確。我想如果用戶名和密碼是正確的,那麼我想在角度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> 

這裏是放出來..

click here to see the result

+0

如果嘗試(pl.data = 「」!) –

+0

對不起仍創下if語句 – Mohammad

+0

空輸入fileld其命中else語句 - – Mohammad

回答

1

在then()塊中,首先嚐試使用以下代碼 console.log(pl.data)檢查pl.data的值。 有了這個,你將會知道從WCF服務返回的值。 如果服務始終返回True,那麼在您的Angular中,它將始終執行if()語句。例如,

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

,如果你發佈到你的HTTP POST請求的響應,這將有助於。我敢打賭,即使有不正確的登錄信息,您仍然會提出有效的http請求,因此會得到一個有效的http響應(數據爲pl.data),可能表明登錄信息不正確。

如果pl.data爲null或未定義,則只會觸擊else語句。研究JavaScript條件以及它們如何與對象一起工作。如果對象存在(不爲空或未定義),則將其視爲true。

+0

應用程序的響應總是顯示錯誤消息.. $ scope.msg =「密碼或用戶名是正確的!」; ..沒關係,如果我發佈了錯誤的用戶名或密碼 – Mohammad

+0

我添加了輸出的結果 – Mohammad

+0

是與空輸入fileld它的命中else語句 – Mohammad