2017-10-11 36 views
1

我正在将Wcf服务用于Angular JS应用程序。我正在通过提供用户名和密码来创建用户登录系统。但是,我在输入中输入的任何用户名或密码都会提示其始终显示消息用户名和密码是否正确。我想如果用户名和密码是正确的,那么我想显示消息在角js应用程序,否则用户名和密码是不正确的,但我不知道为什么我得到同样的消息时,我运行应用程序..根据if和else语句在角度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) { 
       $scope.msg = "Username and password is correct "; 
       // redirect user 
       // $window.location.href = '/Somewhere'; 
      }, function (err) { 
       $scope.msg = "Password Incorrect !"; 
       console.log("Some error Occured" + err); 
      }); 
     }; 

    }]); 

app.service("myService", function ($http) { 
    // Create new record 

    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> 

回答

0

也许你应该在你的服务返回一个对象,例如:

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

或检查你的本地auth API效果很好,这可能会有所帮助。

+0

遗憾还是同样的结果 – Mohammad

0

问题的解决方案只是调试您收到的API响应。你只需要检查你得到的API响应,并相应地显示错误和成功。

我试着用你的本地服务器API调用,并且如预期的那样,它给了我错误,并且显示了“密码不正确”的消息。

要进一步挖掘此问题,您需要提供API响应。

我附上以上截图。

enter image description here

+0

它是代码确定? – Mohammad

+0

我必须在代码中进行任何更改吗? – Mohammad

+0

你可以只提供你使用的脚本代码和html代码 – Mohammad