Angularjs应用程序没有响应Wcf REST服务

问题描述 投票:0回答:1

我正在将WS服务转换为Angular JS应用程序。我正在使用Angular Js Application创建用户登录系统。我在Sql数据库中存储了一些用户信息,如用户名和密码。密码已加密。我正在使用POST方法从sql数据库中检查你的名字和密码,如果用户名和密码id正确我要将用户重定向到欢迎页面,否则用户信息错误我想显示消息用户名或密码id错误。但是,当我单击登录按钮时,它没有响应,没有控制台窗口中的任何错误消息。

这是界面。

  [OperationContract]
        [WebInvoke(Method = "POST",
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json,
        //BodyStyle = WebMessageBodyStyle.WrappedRequest,
        UriTemplate = "/AuthenticateUser")]
        bool AuthenticateUser(UserLogin userLogin);

这是实施..

 public bool AuthenticateUser(UserLogin userLogin)
        {
            // ConfigurationManager class is in System.Configuration namespace
            string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            // SqlConnection is in System.Data.SqlClient namespace
            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand cmd = new SqlCommand("spAuthenticateUser", con);
                cmd.CommandType = CommandType.StoredProcedure;

                //Formsauthentication is in system.web.security
                string encryptedpassword = FormsAuthentication.HashPasswordForStoringInConfigFile(userLogin.Password, "SHA1");

                //sqlparameter is in System.Data namespace
                SqlParameter paramUsername = new SqlParameter("@UserName", userLogin.Username);
                SqlParameter paramPassword = new SqlParameter("@Password", encryptedpassword);

                cmd.Parameters.Add(paramUsername);
                cmd.Parameters.Add(paramPassword);

                con.Open();
                SqlDataReader rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    int RetryAttempts = Convert.ToInt32(rdr["RetryAttempts"]);
                    if (Convert.ToBoolean(rdr["AccountLocked"]))
                    {
                        return true;
                    }
                    else if (RetryAttempts > 0)
                    {
                        int AttemptsLeft = (4 - RetryAttempts);
                        //lblMessage.Text = "Invalid user name and/or password. " +
                        //    AttemptsLeft.ToString() + "attempt(s) left";
                    }
                    else if (Convert.ToBoolean(rdr["Authenticated"]))
                    {
                        return true;
                    }

                }
                return false;
            }
        }

这是脚本代码。

 ///// <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,


            };
            if ($scope.OperType === 1) {
                var promisePost = myService.AuthenticateUser(User);
                promisePost.then(function (pl) {
                    $scope.Id = pl.data.Id;
                    window.location.href = "/Login/WelcomePage";

                    ClearModels();
                }, 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>
<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;">{{Message}}</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>

这是我运行application.Click here to see the result时的屏幕截图

以下是Google Chrome中的网络点击结果。 Click here to see the out put

javascript c# angularjs rest wcf
1个回答
0
投票

看起来您需要将ng-app指令添加到您的html标记中,并在angular.module中定义名称以引导AngularJS应用程序:

<html ng-app="WebClientModule">
© www.soinside.com 2019 - 2024. All rights reserved.